AndEngine - 绘图使用匹配ID级对象/ entites的之间的线 [英] AndEngine - Drawing a Line between Level Objects/Entites using matching IDs

查看:323
本文介绍了AndEngine - 绘图使用匹配ID级对象/ entites的之间的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NVM我忘了补充:

Nvm I forgot to add:

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

所以,现在我希望能够有一个父实体有多个孩子

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"/>

我现在解析child_id作为一个字符串,通过.split将其转换为一个字符串数组(),以及字符串数组转换为int数组

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];

我已经声明childID的在levelObjectUserData到一个数组

I've declared ChildID in levelObjectUserData into an array

public int[] ChildID = {-1};

和我循环通过ButtonSpriteChild数组,看它是否符合ButtonSpriteID

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);

:谢谢你史蒂夫这么多的帮助!还有一件事可以请扩大你的答案的最后一位一点点 - 现在你的XML解析后......,我听不太懂你的榜样((levelObjectUserData)(levelObjects.get(我).getUserData ()))childID的,当我尝试使用它,我得到以下错误:语法错误,将AssignmentOperator前pression完成前pression。并再次感谢你这么多的帮助。

: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

:我也想通了,我的问题的一些XML文件中的实体失踪id和child_ids的第一部分,以及将这些固定误差

: 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.

所以我的问题是,我怎么用id和child_id绘制两个实体之间的线。所以一旦二级对象已匹配我需要得到x和父实体的y值和子实体的x和y值,使得线可以在它们之间绘制即

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);

这就是我如何解析levelObjects

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;
        }
    });

和基于我认为获取父X和Y,我就简单的使用最终诠释x和最终诠释Y,但我不知道我怎么会能够得到孩子x和y。
我希望我已经能够澄清我的问题

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

推荐答案

您有一个选择,

1)可以动态地做到这一点,而你是在加载每个实体,但你需要确保引用到child_id指向已创建previously其他实体。它通过你的XML判断是不是这样的,你的第一个实体的child_id,2,指出这将无法尚未创建第二个实体,所以你不能加入当前的实体之间的线被处理,(1),和这一点。因此,要做到这一点,你就需要重新组织XML,并使用具有不吸引到另一条线的实体开始启动

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.

或2)一旦所有的实体已创建你去通过他们所有,并相应地画线,我觉得会更好,因为它无关紧要什么顺序,然后把它们放在XML。

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.

您选择哪一种永远,你需要能够如果您选择选项2。为此,您需要一个指针引用到每个ButtonSprite也恢复信息来恢复为创建的每个ButtonSprite,其ID也是childID的信息。

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.

您可以将在雪碧,即用户数据的ID和childID的。

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

创建一个名为levelObjectUserData或东西有两个公共整数字段,ID和childID的类。他们设置默认为-1。即,

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;
}

然后您在您的装载机创建的每个levelObject,即,之后

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

levelObject.setCullingEnabled(true);

将类似于以下,

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

现在,每个levelObject添加某种类型的列表,(我使用链表)。即,外

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

levelLoader.registerEntityLoader...

您声明链表

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);

在levelObject添加到列表中,

Add the levelObject to the list,

levelObjects.add(levelObject);

现在你的XML解析后,您的装载机完成后,你可以通过你的链接列表遍历访问每个levelObject创建并绘制它们之间的线条。您可以轻松找到按键精灵的线是从目前的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
            }
        }

}

(其中,i是)0和levelObjects.size(之间的值 - 1)

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

然后找到与该childID的匹配的ID精灵按钮,在列表中再次有,(假设它不会仍然有-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).

您得到孩子x和y,因为这些是雪碧的初始位置创建它们的时候。一旦你有从链表中提及孩子雪碧刚刚打电话,

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()...

其中J是孩子的指数雪碧。

Where j is the index of the child Sprite.

希望这是帮助。

这篇关于AndEngine - 绘图使用匹配ID级对象/ entites的之间的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆