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

2017/4/9

Changing attack directions

AuroraGamer AuroraGamer

2017/4/9

#
Currently in multiple projects my main character can only ever shoot (bullet, arrow, spell, etc) in one direction. I am not rotating the actor, just changing the image;
//move right
if (Greenfoot.isKeyDown("right"))
{
setImage(runR.getCurrentImage);
move(10);
}
// move left
if (Greenfoot.isKeyDown("left"))
{
setImage(runL.getCurrentImage);
move(-10);
}
currently my code for attacking is,
if (Greenfoot.isKeyDown("d") && illusionBooks >= 1 && shotTimer <= 0)
        {
            setImage("Casting.png");
                getWorld().addObject(new FearSpell(), getX()+50, getY()-50);
            shotTimer = 25; // delay next shot
        }
I'm also using the GifImage class to have animated images. So how would I have the projectile move in the direction the actor is facing?
Nosson1459 Nosson1459

2017/4/9

#
Have a variable for the direction, and then move the FearSpell in that direction. In the class that casts you can do:
private int direction = 1;

// if (right arrow is pressed)
    direction = 1;
    // change image
// if (left arrow is pressed
    direction = -1;
    // change image
move(10 * direction);
Then you have a parameter in the FearSpell constructor for setting it's direction and then just having in it's act method either move(speed * direction) or setLocation(getX() + speed * direction, getY()) this variable will only work for the x-axis for the y-axis you'll have to have a separate one.
AuroraGamer AuroraGamer

2017/4/9

#
Thank you, but I decided to just change the images so I could use setRotation.
You need to login to post a reply.