0
votes

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 LOGCAT ERROR

3
Please stop SHOUTING in your titles, and if you want to use the tags in it, make a proper sentence out of it.Mat
(also goes for older questions about this by the way)Nanne
sorry for the SHOUTING (-.-) . I didn't find anything similar to my questionNemoPhobia

3 Answers

1
votes

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

i've solved my second problem!

in the onLoadEngine() method i've add the .setRunOnUpdateThread(true) this way

EngineOptions eo = new EngineOptions(true, ScreenOrientation.LANDSCAPE,
             new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
     eo.getTouchOptions().setRunOnUpdateThread(true);

for futher reference to how remove a sprite properly look here http://code.google.com/p/andengineexamples/source/browse/src/org/anddev/andengine/examples/SpriteRemoveExample.java

0
votes

Try reading this: http://javaprogrammernotes.blogspot.com/2015/04/how-to-put-sprite-on-top-in-andengine.html. It's possible to make sprites on top without hacks with detach/attach but with just setOnTop(true).