如何拾取堆叠在较大精灵上的可拖动小精灵,该精灵也可在引擎中拖动
我的应用程序有问题。我堆叠了一些不同尺寸的精灵(它们的纹理区域也具有不同的尺寸)
如果我将一个小精灵放在一个较大的精灵上,一切似乎都工作正常(小精灵显然位于较大的精灵之上)。如果我想再次拿起小精灵,当我触摸它时,我总是会拖动较大的精灵,即使它的级别低于较小精灵的级别。
我该如何解决这个问题?
我已经尝试将较小的精灵设置为与较大的精灵一样大的区域尺寸,但它不起作用。
为了清楚起见,这里有两个屏幕截图:
红色“E”是我在蓝色“coma”上移动的精灵。彗差较小,因此当我触摸“E”时,我正确地移动了它:
在此第二种情况,蓝色的“D”比“E”大,所以当我触摸它时,我移动“D”而不是“E”
编辑
这是我创建精灵的方法,全部存储在矢量中:
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:
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"
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用精灵的某种 z 排序来判断哪一个在顶部,
即 'd' z 顺序为 1
'e' z 顺序为 0
'e' z 顺序低于 'd' 所以你知道它在第一个对象(对象 N)创建的底部
对象(对象 N)的初始 z 顺序为 0
,每个 附加对象(对象 N)
创建时检查
(如果您计划让许多对象重叠,则需要进行额外的检查),
因此当用户触摸时检查触摸位置是否与对象相交
如果发生多个交叉点,则使用最高 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
(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