Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

java - AndEngine - Drawing a Line between Level Objects/Entites using matching IDs

[Edit 4]Nvm I forgot to add:

childInt_id[i] = Integer.parseInt(childString_id[i]);

[Edit 3]So now I want to be able to have a parent entity have multiple children

 <entity x="640" y="224" type="gamebutton1" id="1" child_id="2, 3"/>

I'm now parsing child_id as a string, converting it to a string array via .split(","), and converting the string array to an int array

final String child_id = SAXUtils.getAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_CHILD_ID);
String[] childString_id = child_id.split(",");
int[] childInt_id = new int[childString_id.length];

I've declared ChildID in levelObjectUserData into an array

public int[] ChildID = {-1};

And I'm looping through the ButtonSpriteChild array to see if it matches ButtonSpriteID

for (int k = 0; k < ButtonSpriteChild.length; k++){
if (ButtonSpriteChild[k] == ButtonSpriteID){ ... } }

I'm not getting any errors when I run the game, but now no lines are being draw between entities. I wanted to ask if I'm missing anything? By the way this is how I'm drawing the lines:

float parentX = levelObjects.get(i).getX();
float parentY = levelObjects.get(i).getY();
float childX = levelObjects.get(j).getX();
float childY = levelObjects.get(j).getY();
Line line = new Line(parentX, parentY, childX, childY, 10,vbom);
line.setColor(Color.GREEN);
attachChild(line);

[Edit 2]:Thank you Steve so much for the help! One more thing could please expand a little bit on the last bit of your answer -"Now after your parsing of the XML...", I have trouble understanding your example "((levelObjectUserData) (levelObjects.get(i).getUserData())).ChildID", when I try to use it I get the following error:"Syntax error, insert "AssignmentOperator Expression" to complete Expression". And again thank you so much for your help

[Edit]: I have figured out the first part of my problem some of the entities from the xml file were missing ids and child_ids, and adding those fixed the error.

So my question becomes, how do I use "id" and "child_id" to draw a line between two entities. So once two level objects have been matched I need to get the x and y value of the parent entity and the x and y value of the child entity so that a line can be drawn between them i.e

<entity x="640" y="224" type="gamebutton1" id="1" child_id="2"/> 
<entity x="512" y="224" type="gamebutton2" id="2"/>
Line line = new Line(parentX, parentY, childX, childY, 5, vbom);

So this is how I'm parsing the levelObjects

levelLoader.registerEntityLoader(new EntityLoader<SimpleLevelEntityLoaderData>(TAG_ENTITY) {

        @Override
        public IEntity onLoadEntity(String pEntityName, IEntity pParent, Attributes pAttributes, SimpleLevelEntityLoaderData pEntityLoaderData) throws IOException {
            final int x = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_X);
            final int y = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_Y);
            final String type = SAXUtils.getAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_TYPE);
            final int id = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_ID);
            final int child_id = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_CHILD_ID);

            final ButtonSprite levelObject;

            if (type.equals(TAG_ENTITY_ATTRIBUTE_VALUE_GAMEBUTTON1)) {
                levelObject = new ButtonSprite(x, y, resourcesManager.gamebutton1_region, vbom, new OnClickListener() {

                    @Override
                    public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {                           

                    }
                });
            }

            else if (type.equals(TAG_ENTITY_ATTRIBUTE_VALUE_GAMEBUTTON2)) {
                levelObject = new ButtonSprite(x, y, resourcesManager.gamebutton2_region, resourcesManager.gamebuttonpressed_region, vbom, new OnClickListener() {

                    @Override
                    public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {

                    }
                });
            }

            else if (type.equals(TAG_ENTITY_ATTRIBUTE_VALUE_GAMEBUTTON3)) {
                levelObject = new ButtonSprite(x, y, resourcesManager.gamebutton3_region, resourcesManager.gamebuttonpressed_region, vbom, new OnClickListener() {

                    @Override
                    public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {

                    }
                });
            }

            else if (type.equals(TAG_ENTITY_ATTRIBUTE_VALUE_GAMEBUTTON4)) {
                levelObject = new ButtonSprite(x, y, resourcesManager.gamebutton4_region, resourcesManager.gamebuttonpressed_region, vbom, new OnClickListener() {

                    @Override
                    public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {

                    }
                });
            }

            else if (type.equals(TAG_ENTITY_ATTRIBUTE_VALUE_GAMEBUTTON5)) {
                levelObject = new ButtonSprite(x, y, resourcesManager.gamebutton5_region, resourcesManager.gamebuttonpressed_region, vbom, new OnClickListener() {

                    @Override
                    public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {

                    }
                });
            }

            else if (type.equals(TAG_ENTITY_ATTRIBUTE_VALUE_GAMEBUTTON6)) {
                levelObject = new ButtonSprite(x, y, resourcesManager.gamebutton6_region, resourcesManager.gamebuttonpressed_region, vbom, new OnClickListener() {

                    @Override
                    public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {

                    }
                });
            }

            else {
                throw new IllegalArgumentException();
            }

            levelObject.setCullingEnabled(true);
            registerTouchArea(levelObject);
            setTouchAreaBindingOnActionDownEnabled(true);
            return levelObject;
        }
    });

And based on that I assume that getting the parent x and y, I would simple use final int x and final int y, but I'm not sure how I would be able to get the child x and y. I hope I've been able to clarify my problem

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a choice,

1) You can do this dynamically while you are loading in each entity but you need to make sure the reference to the other entity which child_id is pointing to has been created previously. Which judging by your XML is not the case, your first entity's child_id, 2, points to the second entity which will not be created yet and so you can not join a line between the current entity being processed, (1), and that. So to do this you would need to reorganise your XML and start with an entity that has no line drawn to another initially.

or 2) Once all entities have been created you go through them all and draw lines accordingly which I feel would be better as it does not matter what order you then put them in the XML.

Which ever you choose, you need to be able to recover information for each ButtonSprite created, its ID and also the ChildID if you choose option 2. For this you need a pointer reference to each ButtonSprite also to recover the information.

You can place the ID and ChildID in the user data for the Sprite, i.e.,

Create a class called levelObjectUserData or something which has two public integer fields, ID and ChildID. Set them to -1 by default. i.e.,

public class levelObjectUserData {
    public int ID = -1;
    public int ChildID = -1;
}

Then for each levelObject you create in your loader, ie., after

levelObject.setCullingEnabled(true);

Put something like the following,

final levelObjectUserData MyData = new levelObjectUserData ();
MyData.ID = id;
MyData.ChildID = child_id;
levelObject.setUserData(MyData);

Now, add each levelObject to a list of some sort, (I use linked list). i.e, outside of

levelLoader.registerEntityLoader...

Declare your linked list

LinkedList<Shape> levelObjects = new LinkedList<Shape>();

Then, every time you create one in your loader add it to the list, i.e., after

levelObject.setCullingEnabled(true);

Add the levelObject to the list,

levelObjects.add(levelObject);

Now after your parsing of the XML, after your loader has finished, you can access each levelObject you created by iterating through your linked list and draw lines between them. You can easily find the button sprite the line is to be drawn to from the current levelObject by getting the user data for that item, i.e.,

for (int i = 0;i<levelObjects.size();i++)
{

        final int ButtonSpriteChild  = ((levelObjectUserData) (levelObjects.get(i).getUserData())).ChildID;
        for (int j = 0;j<levelObjects.size();j++)
        {
            final int ButtonSpriteID = ((levelObjectUserData) (levelObjects.get(j).getUserData())).ID;

            if (ButtonSpriteChild  == ButtonSpriteID)
            {
                //Draw your line here between ButtonSprite levelObjects.get(i)
                //and ButtonSprite levelObjects.get(j) - the child that matches up
            }
        }

}

(Where i is a value between 0 and levelObjects.size() - 1)

Then locate the sprite button with the ID that matches the ChildID you have in the list once more, (assuming it does not still have the default value of -1).

You get the child x and y as these are the initial positions of the Sprite when they were created. Once you have the reference to the child Sprite from the linked list just call,

levelObjects.get(j).getInitialX()...
levelObjects.get(j).getInitialy()...

Where j is the index of the child Sprite.

Hope this is of help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...