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

2015/7/28

Space Invader direction change

vondeser vondeser

2015/7/28

#
hello all , first time using greenfoot. i managed to get the aliens to move to the right and left but when once they touch the left wall they dont go back right , they just keep going left, ive tired if statements and loops , nothings working. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class SpaceShip here. * * @author Aditya Wagh * @version 10 */ public class Invader extends Actor { private int speed = 1; private double heading = 0 ; private int health; private Invader Invaders; public int shouldMove = 0; public int fireBullet; public int chance = 1; public GreenfootImage image1; public GreenfootImage image2; private int aliensKilled; // instant variables here to indivate 2 different images, so that you can use these // in flipimage method // you need to have a constructor with 2 image parameters public void move() { double angle = Math.toRadians(heading); // change heading to 90 degrees to move down int x = (int) Math.round(getX() + Math.cos(angle) * speed); int y = (int) Math.round(getY() + Math.sin(angle) * speed); // can change the speed setLocation(x, y); } public void gotHit(){ // decrease health point. } public void destroy(){ Greenfoot.playSound("explosion.wav"); } public void fireBullet(){ if (shouldMove == 1){ fireBullet = Greenfoot.getRandomNumber(100); if (fireBullet <= chance) { getWorld().addObject(new InvaderBullet(), getX(), getY()+5); } } } public void act(){ flipImage(); fireBullet(); destroy(); } public void flipImage(){ //TODO if (getImage()== image1){ setImage(image2); } else { setImage(image1); } } public void moveDown(){ heading = 90; speed = 1; move(); // 1. change the heading to 90 deg // 2. change the speed // 3. call move() } public void reverseDirection(){ heading = 180; //move(); //move right, heading = 0 , move left = 180 } public int getHealth(){ //TODO return 3; } public int getPoints(){ //TODO // i return 10; } public boolean atWorldEdge() { if (getX() < 40 || getX() > getWorld().getWidth() - 41) { return true; } if (getY() < 40 || getY() > getWorld().getHeight() - 41) { return true; } else { return false; } } }
davmac davmac

2015/7/28

#
Welcome. Please read the guidelines for posting code: Regarding the code you did post, the problem seems to be that you never change the 'heading' which seems to control the direction of movement. There should be something like this:
if (atWorldEdge()) {
    if (heading == 0) {
        heading = 180;
    }
    else {
        heading = 0;
    }
}
vondeser vondeser

2015/7/28

#
Thank you for replying, I will check when I get home, and will read the rules.
vondeser vondeser

2015/7/29

#
OK now what happens is only the first row seems to be affected by the statement, the rest of the aliens don't go left they drop down after hitting the right wall.
You need to login to post a reply.