This site requires JavaScript, please enable it in your browser!
Greenfoot back
Baenefin
Baenefin wrote ...

2013/10/29

Image swapping on collision

Baenefin Baenefin

2013/10/29

#
Hello all, I would like to make my character show a certain image (call it Random.png for now) when she hits an enemy. When she doesn't hit an enemy she shows the normal image. How do I implement that this is what I have:
public class Alice extends Mover
{
public void act() 
    {
        checkKeys();   
        checkCollisionEnemy();
}
private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            setImage("AliceLeft.png");
            moveLeft();
        }
        if (Greenfoot.isKeyDown("right")  )
        {
            setImage("AliceRight.png");
            moveRight();
        }
}

public void checkCollisionEnemy()  
    {  
        Actor enemy = getOneObjectAtOffset(0, getImage().getHeight()/2+5, Walker.class); 
        if (enemy != null) getWorld().removeObject(enemy);  
        Actor collided = getOneIntersectingObject(Enemy.class);  
        if (collided !=null && contactTimer ==0)  
        {  
            ((Score)getWorld().getObjects(Score.class).get(0)).add(-1);
            contactTimer = 100;  
        }  

    }  

    public void runCollisionTimer()    
    {    
        if (contactTimer > 0) contactTimer --;    
    }
}
danpost danpost

2013/10/29

#
Since you are are 'swapping' images for left and right facing, you will have to put a condition on having them set. Add a instance boolean field called 'onEnemy'. Set it to true and change the image when the field is false and an enemy is detected. Set it to false if the field is true and no enemy is detected. Do not set the image for left or right if the field is true.
Baenefin Baenefin

2013/10/29

#
I will work on that. Thanks for the pointer. I will let you know what I come up with :)
Baenefin Baenefin

2013/10/29

#
I have something, but I am not grasping it completely. Added
public class Alice extends Mover
{
    boolean onEnemy =false;

public void act() 
    {
        checkKeys(); 
}
private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            moveLeft();
            if (onEnemy !=true){
                setImage("AliceLeft.png");
            }
        }
        if (Greenfoot.isKeyDown("right")  )
        {
            moveRight();
            if (onEnemy !=true){
                setImage("AliceRight.png");
            }
        }

        if (Greenfoot.isKeyDown("up") )
        {
            if (onEnemy !=true)
                if (onGround() || onMover()) 
                    jump();
        }
    public void checkCollisionEnemy()  
    {  

        Actor enemy = getOneObjectAtOffset(0, getImage().getHeight()/2+5, Walker.class); 
        if (enemy != null) 
        {       
            getWorld().removeObject(enemy);  
            ((enemyCounter)getWorld().getObjects(enemyCounter.class).get(0)).add(-1);
        }
        Actor collided = getOneIntersectingObject(Enemy.class);  
        if (collided !=null && contactTimer ==0)  
        {  
            if(onEnemy = true)
            {
                setImage("Random.png");
            }

            ((Score)getWorld().getObjects(Score.class).get(0)).add(-1);
            contactTimer = 100;  
        }  
    }
Baenefin Baenefin

2013/10/29

#
This turns me into random image but never turns me back. Also when i am random image i cant jump.
danpost danpost

2013/10/29

#
Remove line 28 to jump again. And you need to adjust your conditions to what I gave above for setting and clearing the boolean and for its use with the Random.png image. The conditions I see right now are: (1) detected walker enemy (2) detected enemy enemy and timer is exhausted (3) detected enemy enemy and timer is exhausted and previously detected enemy (onEnemy == true) Line 44 should be 'if (onEnemy == true)', using a double equal sign to be a proper conditional expression (this comment is not intended to infer whether that line should be there or not).
Baenefin Baenefin

2013/10/30

#
I can't seem to get it. She now does not shance image. I don't think I am grasping the logic.
public class Alice extends Mover
{
boolean onEnemy = true;

private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left") )
        {
            moveLeft();
            if (onEnemy !=true){
                setImage("AliceLeft.png");
            }
        }
        if (Greenfoot.isKeyDown("right")  )
        {
            moveRight();
            if (onEnemy !=true){
                setImage("AliceRight.png");
            }
        }

        if (Greenfoot.isKeyDown("up") )
        {
            if (onGround() || onMover()|| onUBlock() ) 
                jump();
        }
}

       public void checkCollisionEnemy()  
    {  
        Actor enemy = getOneObjectAtOffset(0, getImage().getHeight()/2+5, Walker.class); 
        if (enemy != null) 
        {    
            getWorld().removeObject(enemy);  
            ((enemyCounter)getWorld().getObjects(enemyCounter.class).get(0)).add(-1);
        }

        Actor collided = getOneIntersectingObject(Enemy.class);  
        if (collided !=null && contactTimer ==0)  
        {  
            if (onEnemy == true)
            {
                setImage("Random.png");
            }
            ((Score)getWorld().getObjects(Score.class).get(0)).add(-1);
            contactTimer = 100;  
        }  
        else
        {
            onEnemy= false;
        }
    }

    public void runCollisionTimer()    
    {    
        if (contactTimer > 0) contactTimer --;    
    }
}
danpost danpost

2013/10/30

#
Your logic needs work.
public void checkCollisionEnemy()  
{  
    Actor enemy = getOneObjectAtOffset(0, getImage().getHeight()/2+5, Walker.class); 
    if (enemy != null) 
    {    
        getWorld().removeObject(enemy);  
        ((enemyCounter)getWorld().getObjects(enemyCounter.class).get(0)).add(-1);
    }
    Actor collided = getOneIntersectingObject(Enemy.class);  
    if (collided !=null)
    {
        if (!onEnemy)
        {
            onEnemy = true;
            setImage("Random.png");
        }
        if (contactTimer == 0)
        {
            ((Score)getWorld().getObjects(Score.class).get(0)).add(-1);
            contactTimer = 100;
        }
    }  
    else if (onEnemy)
    {
        onEnemy = false;
        // set image back?
    }
}
You need to login to post a reply.