Its me again. This time i want to let my aliens shoot randomly. i have looked up a lot of codes, but i can't find any that works for me. Its for my school project. i have to hand it in tomorrow, i realize that i had to start earlier :(.
In my code i tried to let the aliens shoot with a key but even that doesn't work.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class alien extends Actor { int tempX,tempY; int length = 0; int line = 0; int X = 5; int Y = 0; boolean removed = false; int Number = 0; public void act() { if(removed){return;} SetSpeed(); movement(); schootRocket(); if(line>0) nextLine(); } public void movement() { tempX = getX(); tempY = getY(); setLocation(tempX+X, tempY+Y); } public void SetSpeed() { if(getX()>=595 & X == 5) { line = 1; X = -5; } if(getX()<=5 & X == -5) { line = 1; X = 5; } } public void nextLine() { Y = 40-15; line = 0; setLocation(tempX+X, tempY+Y); Y = 0; } public void schootRocket() { String knop; knop=Greenfoot.getKey(); if ( knop=="x") { getWorld().addObject(new kogel(), getX(), getY()-5); } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class kogel extends Actor { public void act() { setLocation(getX(),getY()-5); aanRandWereld(); } public void eatrocket() { Actor rocket = getOneObjectAtOffset(0, 0, rocket.class); if(rocket != null) { getWorld().removeObject(rocket); } } public void aanRandWereld() { if(atWorldEdge()) { getWorld().removeObject(this); } } public boolean atWorldEdge() { if(getX() < 10 || getX() > getWorld().getWidth() - 10) return true; if(getY() < 10 || getY() > getWorld().getHeight() - 10) return true; else return false; } }