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

2013/5/12

shooting space invaders

LittleJacob LittleJacob

2013/5/12

#
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; }
}
danpost danpost

2013/5/12

#
One thing is you need to change line 61 of the alien class to
if ("x".equals(knop))
A string is an object, and if you compare objects with '==', you read it as "is the same instance as". The instance from the keyboard is not the same instance as that coming from the code itself.
danpost danpost

2013/5/12

#
Line 11 of the kogel class will make the kogel objects move 'up' the screen (toward the top of the window). If this is supposed to be a clone of any normal space-invader game, the shots from the aliens should be traveling 'down' the screen.
danpost danpost

2013/5/12

#
If your kogel objects are not to have any horizontal movement and they only travel down the screen, your 'atWorldEdge' method can be simplified to:
public boolean atWorldEdge()
{
    return getX()>getWorld().getHeight()-10;
}
LittleJacob LittleJacob

2013/5/12

#
Thanks for the quick replies. If setLocation(getX(),getY()-5); is moving up how will the code be if it should go down
GreenGoo GreenGoo

2013/5/12

#
setLocation(getX(), getY() + 5)
LittleJacob LittleJacob

2013/5/12

#
Thnks greengoo
LittleJacob LittleJacob

2013/5/12

#
I have changed everything mentioned above but still the aliens don't shoot
danpost danpost

2013/5/12

#
It may not help your situation, but change line 63 of the 'alien' class to:
getWorld().addObject(new kogel(), getX(), getY());
LittleJacob LittleJacob

2013/5/12

#
nope it didn't work. I might as well upload it. If maybe one of u guys had time could take a look at it.
LittleJacob LittleJacob

2013/5/12

#
http://www.greenfoot.org/scenarios/8321
danpost danpost

2013/5/12

#
Will do shortly.
LittleJacob LittleJacob

2013/5/12

#
THANKKS i appreciate it
danpost danpost

2013/5/12

#
Ok. The main problem is that, right now, you have two objects that require 'getKey' and one is hogging up all the keystrokes and keeping them from the other. You know, already, that you will not require the 'x' key for the aliens to shoot, so let us find another way to trigger the aliens to shoot. Simplest, would be a random number. Try this in your 'schootRocket' method (instead of what you have there):
if (Greenfoot.getRandomNumber(100) < 1) getWorld().addObject(new kogel(), getX(), getY());
LittleJacob LittleJacob

2013/5/12

#
Thank you it works now
You need to login to post a reply.