如何拾取堆叠在较大精灵上的可拖动小精灵,该精灵也可在引擎中拖动

发布于 2024-12-19 04:09:29 字数 4784 浏览 6 评论 0原文

我的应用程序有问题。我堆叠了一些不同尺寸的精灵(它们的纹理区域也具有不同的尺寸)

如果我将一个小精灵放在一个较大的精灵上,一切似乎都工作正常(小精灵显然位于较大的精灵之上)。如果我想再次拿起小精灵,当我触摸它时,我总是会拖动较大的精灵,即使它的级别低于较小精灵的级别。

我该如何解决这个问题?

我已经尝试将较小的精灵设置为与较大的精灵一样大的区域尺寸,但它不起作用。

为了清楚起见,这里有两个屏幕截图:

红色“E”是我在蓝色“coma”上移动的精灵。彗差较小,因此当我触摸“E”时,我正确地移动了它:

si

在此第二种情况,蓝色的“D”比“E”大,所以当我触摸它时,我移动“D”而不是“E”

si

编辑

这是我创建精灵的方法,全部存储在矢量中:

public Vector<Sprite> initSprite(Vector<TextureRegion> tRegionV, final Scene se, Context c){
    Vector<Sprite> aux = new Vector<Sprite>();
    vRes.posizioni = vRes.init();
    
    
    for(int i=0; i<dataV.size(); i++ ){         
        final int gap = dataV.elementAt(i).gap+data[3];  //creo qui la differenza per il dito
        final float sX = dataV.elementAt(i).x;
        final float sY = dataV.elementAt(i).y;;
        final String label = dataV.elementAt(i).label;
        final Context co = c;
        
        sprite = new Sprite(dataV.elementAt(i).x, dataV.elementAt(i).y, tRegionV.elementAt(i))
        
        // gestione del drag & drop
        {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                    final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                    
                    

                if(pSceneTouchEvent.isActionDown()){
                    //TOCCO DELLO SPRITE
                    
                    moved=false;
                    
                    startY=getY();
                    startX=getX();
                    
                    if(this.getParent()!=se.getChild(3) && this.getParent()==se.getChild(1)){
                        
                        
                        ChangeLayer(se, this, 1, 3);
                    }
                    else if(this.getParent()!=se.getChild(3) && this.getParent()==se.getChild(2)){
                        
                        
                        ChangeLayer(se, this, 2, 3);
                    }
                             
              
                    
                    MoveYModifier mod = new MoveYModifier(0.1f, getY(), pSceneTouchEvent.getY()- gap);
                    this.registerEntityModifier(mod);
                }
                
                if(pSceneTouchEvent.isActionMove()){
                    //MOVIMENTO DELLO SPRITE
                    moved=true;
                    setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY()
                        - gap /*this.getHeight() / 2*/);
                }

                if(pSceneTouchEvent.isActionUp()){
                    //RILASCIO DELLO SPRITE
                    
                    float x =getX() + (getWidth()/2);
                    float y =getY() + (getHeight()/2);
                    
                    if(y<=285){
                    
                        if(y>(sY+ getHeight()/2)-20 && y<(sY+ getHeight()/2)+20 && x>(sX+ getWidth()/2)-20 && x<(sX+ getWidth()/2)+20){
                            
                            if(this.getParent()!=se.getChild(1) && this.getParent()==se.getChild(2)){
                                ChangeLayer(se, this, 2, 1);

                            }
                            return true;
                        }
                        
                        else {
                            val=vRes.fallInCell(x,y,this.getHeight(),label);
                            setPosition(val[0]-(this.getWidth()/2),val[1]-(this.getHeight()/2)+6);
                        }
                    }
                    // if che se ho solo toccato e si è alzata, deve tornare dove stava
                    if(moved==false){
                        MoveYModifier mod = new MoveYModifier(0.1f, getY(), startY);
                        this.registerEntityModifier(mod);
                        setPosition(startX, startY);
                    }
                    
                    if(this.getParent()!=se.getChild(1) && this.getParent()==se.getChild(3)){
                        
                        ChangeLayer(se, this, 3, 1);
                    }
                    else if(this.getParent()!=se.getChild(1) && this.getParent()==se.getChild(2)){
                        
                        ChangeLayer(se, this, 2, 1);
                    }
                }
                return true;
            }
        };
        
        aux.addElement(sprite);
    }
    
    return aux;
}

I've got a problem with my app. I have stacked some sprites of different dimensions (their texture regions are also of different dimensions)

If I drop a small sprite on a bigger one, it all seems to work fine (the small sprite is clearly above the bigger one). If I want to pick up again the small sprite, when I touch it, I always end up dragging the bigger sprite, even if it's level is in a lower level than the smaller sprite's level.

How can I fix this?

I already tried to set to the smaller sprite a region dimension as big as the bigger sprite's, but it didn't work.

Just to be clear, here are two screenshots:

The red "E" is the sprite I moved on the blue "coma". The coma is smaller so when I touch the "E" I moved it correctly:

si

In this second case, the blue "D" is bigger than the "E" so when I touch it I move a "D" and not the "E"

si

EDIT

Here is how I create my sprites, all stored in a vector:

public Vector<Sprite> initSprite(Vector<TextureRegion> tRegionV, final Scene se, Context c){
    Vector<Sprite> aux = new Vector<Sprite>();
    vRes.posizioni = vRes.init();
    
    
    for(int i=0; i<dataV.size(); i++ ){         
        final int gap = dataV.elementAt(i).gap+data[3];  //creo qui la differenza per il dito
        final float sX = dataV.elementAt(i).x;
        final float sY = dataV.elementAt(i).y;;
        final String label = dataV.elementAt(i).label;
        final Context co = c;
        
        sprite = new Sprite(dataV.elementAt(i).x, dataV.elementAt(i).y, tRegionV.elementAt(i))
        
        // gestione del drag & drop
        {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                    final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                    
                    

                if(pSceneTouchEvent.isActionDown()){
                    //TOCCO DELLO SPRITE
                    
                    moved=false;
                    
                    startY=getY();
                    startX=getX();
                    
                    if(this.getParent()!=se.getChild(3) && this.getParent()==se.getChild(1)){
                        
                        
                        ChangeLayer(se, this, 1, 3);
                    }
                    else if(this.getParent()!=se.getChild(3) && this.getParent()==se.getChild(2)){
                        
                        
                        ChangeLayer(se, this, 2, 3);
                    }
                             
              
                    
                    MoveYModifier mod = new MoveYModifier(0.1f, getY(), pSceneTouchEvent.getY()- gap);
                    this.registerEntityModifier(mod);
                }
                
                if(pSceneTouchEvent.isActionMove()){
                    //MOVIMENTO DELLO SPRITE
                    moved=true;
                    setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY()
                        - gap /*this.getHeight() / 2*/);
                }

                if(pSceneTouchEvent.isActionUp()){
                    //RILASCIO DELLO SPRITE
                    
                    float x =getX() + (getWidth()/2);
                    float y =getY() + (getHeight()/2);
                    
                    if(y<=285){
                    
                        if(y>(sY+ getHeight()/2)-20 && y<(sY+ getHeight()/2)+20 && x>(sX+ getWidth()/2)-20 && x<(sX+ getWidth()/2)+20){
                            
                            if(this.getParent()!=se.getChild(1) && this.getParent()==se.getChild(2)){
                                ChangeLayer(se, this, 2, 1);

                            }
                            return true;
                        }
                        
                        else {
                            val=vRes.fallInCell(x,y,this.getHeight(),label);
                            setPosition(val[0]-(this.getWidth()/2),val[1]-(this.getHeight()/2)+6);
                        }
                    }
                    // if che se ho solo toccato e si è alzata, deve tornare dove stava
                    if(moved==false){
                        MoveYModifier mod = new MoveYModifier(0.1f, getY(), startY);
                        this.registerEntityModifier(mod);
                        setPosition(startX, startY);
                    }
                    
                    if(this.getParent()!=se.getChild(1) && this.getParent()==se.getChild(3)){
                        
                        ChangeLayer(se, this, 3, 1);
                    }
                    else if(this.getParent()!=se.getChild(1) && this.getParent()==se.getChild(2)){
                        
                        ChangeLayer(se, this, 2, 1);
                    }
                }
                return true;
            }
        };
        
        aux.addElement(sprite);
    }
    
    return aux;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2024-12-26 04:09:29

您可以使用精灵的某种 z 排序来判断哪一个在顶部,

即 'd' z 顺序为 1
'e' z 顺序为 0

'e' z 顺序低于 'd' 所以你知道它在第一个对象(对象 N)创建的底部

对象(对象 N)的初始 z 顺序为 0

,每个 附加对象(对象 N)
创建时检查

 if (Object N) intersects with a previous object (object N-1).
     (object N).z-order = (object N-1).z-order+1,

(如果您计划让许多对象重叠,则需要进行额外的检查),

因此当用户触摸时检查触摸位置是否与对象相交
如果发生多个交叉点,则使用最高 z 阶对象

You could use some sort z ordering of sprites to tell which one is on top

ie 'd' z order is 1
'e' z order is 0

'e' z order is lower than 'd' so you know its on the bottom

on the first object (object N)creation, the initial z order is 0 for that object(object N)

for each additional object (object N)
when created check

 if (Object N) intersects with a previous object (object N-1).
     (object N).z-order = (object N-1).z-order+1,

(there will be additional checks needed if you plan on having many object overlapping)

so when a user touches check if the touch location intersects with an object(s)
if more than one intersection occurs use the highest z order object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文