In my app I have some draggable sprite some in the same level, other divided in different layers, all stacked one over another. when I drag one I need that this sprite appear always on foregorund.
If I take a sprite that was on the second layer (for example) and I need to place it over another letter that is in the first layer, I need that the one I'm touching appears over the second letter. I think I need to change the layer programmatically in the "onAreaTouched" in an if like
if(pSceneTouchEvent.isActionDown()) {
//TOCCO DELLO SPRITE
}
but how? or does exist a command to set the current touched sprite as foregorund?
EDIT According to skyuzo I tried this way. in my main activity I defined two different layer, one for all the sprites and another as a foreground
Entity base = new Entity();
Entity foreground = new Entity();
scene.attachChild(base);
scene.attachChild(foreground);
spriteV = vRes.initSprite(tRegionV, scene, base, foreground); //this invoce a method that initialize a vector containing all my sprites
for(int x=0;x<spriteV.size();x++){
base.attachChild(spriteV.elementAt(x)); //here i attach every sprite to the scene
}
in another class I've defined the onAreaTouched for each sprite and I've done this way
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()){
//TOCCO DELLO SPRITE
if(getY()<=261 )
setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY()
- gap);
}
//switch layer
base.detachChild(sprite);
fore.attachChild(sprite);
}
if(pSceneTouchEvent.isActionMove()){
//MOVIMENTO DELLO SPRITE
setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY()
- gap);
}
if(pSceneTouchEvent.isActionUp()){
//RILASCIO DELLO SPRITE
float x =getX();
float y =getY();
if(y<=261 ){
if(y>sY-20 && y<sY+20 && x>sX-20 && x<sX+20){
return true;
}
else {
val=fallInCell(x,y,this.getHeight());
setPosition(val[0],val[1]);
}
}
//switch layer
fore.detachChild(sprite);
base.attachChild(sprite);
}
return true;
}
};
aux.addElement(sprite);
}
return aux;
}
but when I touch a sprite my app a force close.here the logcat error
You could have two layers: One containing all of your non-foreground sprites and one containing your foreground sprite.
Example
void initScene() {
scene.attachChild(spriteLayer);
scene.attachChild(foregroundLayer);
}
void spriteTouched(Sprite sprite) {
spriteLayer.detachChild(sprite);
foregroundLayer.attachChild(sprite);
}
void spriteUntouched(Sprite sprite) {
foregroundLayer.detachChild(sprite);
spriteLayer.attachChild(sprite);
}