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

2019/1/19

image in other class

Simson1000 Simson1000

2019/1/19

#
Hello,I have a problem with my new game.My "shooter1" is changing his image when I'm going left.And now i want that my bullet is changing his direction when i change my image and shoot
danpost danpost

2019/1/19

#
Simson1000 wrote...
Hello,I have a problem with my new game.My "shooter1" is changing his image when I'm going left.And now i want that my bullet is changing his direction when i change my image and shoot
Please show class codes for shooting actor.
Simson1000 Simson1000

2019/1/22

#
public void act() 
    {
     if(Greenfoot.isKeyDown("d")){
        setLocation(getX() + 5,getY());
        setImage("pixil-frame-0.png");
        }
        if(Greenfoot.isKeyDown("a")){
        setLocation(getX() - 5,getY());
        setImage("links.png");
        
        }
        if(Greenfoot.isKeyDown("w")){
        setLocation(getX(),getY() -5);
        }
        if(Greenfoot.isKeyDown("s")){
        setLocation(getX(),getY() + 5);
        }
        if(Greenfoot.isKeyDown("space")){
    getWorld().addObject(new bullet(),getX(),getY());
    }
    }
Simson1000 Simson1000

2019/1/22

#
"links" is one of my skins :D
danpost danpost

2019/1/22

#
As is, there is no way to determine which image was last set to the actor. You will probably need to add a field so you can track the last direction set. Easiest would be an int field, giving it values of 0 through 3, for right, down, left and up (facing angle divided by 90). Then, when you create a bullet, you can follow up with setting its direction properly:
Actor bullet = new Bullet();
bullet.setRotation(90*<< direction value >>);
getWorld().addObject(bullet, getX(), getY());
Simson1000 Simson1000

2019/1/23

#
and with a variable?
danpost danpost

2019/1/23

#
Simson1000 wrote...
and with a variable?
Yeah:
private int facingDir;

public void act()
{
    if (Greenfoot.isKeyDown("d")){
        setLocation(getX()+5, getY());
        setImage("pixil-frame-0.png");
        facingDir = 0;
    }
    // etc.
}
You need to login to post a reply.