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

2014/12/18

Help Please, New to Coding

xJesterPROx xJesterPROx

2014/12/18

#
I'm having trouble with my animating, right now i have an Enemy that moves towards the left, and when it hits a wall, it moves down a little and begins to move towards the right, like Space Invaders. My problem is that my alien only faces towards the right, even when its moving left, can you show me how to make it face the direction that its moving? I already have 2 images, one where it faces towards the left and right. Here's what i got so far. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Alien here. * * @author (your name) * @version (a version number or a date) */ public class Alien extends Bad_Guys { public int SpeedX = 5; public void act() { move(SpeedX); checkWalls(); if (Greenfoot.getRandomNumber(1000) > 990) { getWorld().addObject(new Bullets(), getX(), getY()); } } public void checkWalls() { if (atWorldEdge()) { SpeedX = SpeedX*-1; setLocation(getX(), getY() + 20); } } }
xJesterPROx xJesterPROx

2014/12/18

#
sorry, it moves towards the right first, then the left, if that affects anything
danpost danpost

2014/12/19

#
The problem is that if the alien was already at the edge when it drops down a little, then it will still be at the edge the next act cycle unless you move it back off the edge. You could, as an alternative, check each side edge individually (instead of using the 'atWoldEdge' method) and use a combination of two states (the direction and the location) for the conditions to reverse direction -- if moving left and at left edge or if moving right and at right edge, turn around.
xJesterPROx xJesterPROx

2014/12/19

#
im confused, can you show me an example?
danpost danpost

2014/12/19

#
Just move it back off the edge. Instead of 'setLocation(getX(), getY()+20);' use 'setLocation(getX()+speedX, getY()+20);'. Then you can ask 'if (speedX > 0)...else...' for setting the appropriate image.
xJesterPROx xJesterPROx

2014/12/19

#
Thank you! It works!
You need to login to post a reply.