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

2012/7/11

AirForce

1
2
3
4
KommandoFaust KommandoFaust

2012/7/11

#
NEED HELP! 1) I made a basic aircraft game. The airplane can shoot and move, to the left and right, but i want it to move up and down too. Is there a simple script for that? 2) I wan't another plane to explode on impact with the shot, i already got the plane to explode with "void explode()" but not on impact. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Plane here. * * @author (your name) * @version (a version number or a date) */ public class Plane extends Actor { /** * Act - do whatever the Plane wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("right")) { move(1); } if(Greenfoot.isKeyDown("left")) { move(-1); } if(Greenfoot.isKeyDown("space")) { fire(); } } /** * fire the cannon */ private void fire() { shot shot = new shot(); getWorld().addObject(shot, getX(), getY()); shot.setRotation(270); shot.move(12.0); } }
MatheMagician MatheMagician

2012/7/11

#
If you just want to move in east, west, north south, directions when the arrow keys are pressed, this code will work.
if(Greenfoot.isKeyDown("right")) {
        setLocation(getX()+1,getY());
        }    
else if(Greenfoot.isKeyDown("left")) {
        setLocation(getX()-1,getY());
        }    
else if(Greenfoot.isKeyDown("up")){
         setLocation(getX(),getY()-1);
}
else if(Greenfoot.isKeyDown("down"){
          setLocation(getX(),getY()+1);
}
I you want to rotate when you press the up and down keys, you can just add:
if(Greenfoot.isKeyDown("up")) {
        setRotation(getRotation()-1);
        }    
        if(Greenfoot.isKeyDown("down")) {
        setRotation(getRotation()-1);
        }    
to your existing code.
KommandoFaust KommandoFaust

2012/7/11

#
Thank you for your fast answer! :)
MatheMagician MatheMagician

2012/7/11

#
Oops, that last bit of code is supposed to be:
if(Greenfoot.isKeyDown("up")) {  
        setRotation(getRotation()-1);  
        }      
        if(Greenfoot.isKeyDown("down")) {  
        setRotation(getRotation()+1);  
        }
MatheMagician MatheMagician

2012/7/11

#
Now, as to your second question, I believe if you put this code at the end of the enemy planes act cycle, he should explode.
if(getOneIntersectingObject(shot.class))
{
          explode();
}
KommandoFaust KommandoFaust

2012/7/11

#
Movment and shooting is just fine now :) , but i got errors when i add this : if(getOneIntersectingObject(shot.class)) { explode(); } Maybe I should rename the shot class to shot? I will try smth. right now. Hope it works.
::::::::THE ENEMY PLANE LOOKS LIKE THIS::::::::::

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class p2e here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class p2e extends Actor
{
    private static final int NUM_FRAGMENTS = 1;
    /**
     * Act - do whatever the p2e wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    public void explode()
    {
        placep2edes (getX(), (getY()), NUM_FRAGMENTS);
        getWorld().removeObject(this);
}

private void placep2edes(int x, int y, int numfragments)
{
 
        getWorld().addObject (new p2edes(), x, y );
    }
}  
KommandoFaust KommandoFaust

2012/7/11

#
Maybe this will help you to co-ordinate me :) http://postimage.org/image/oi9f5whi3/
MatheMagician MatheMagician

2012/7/11

#
It would help if you posted the errors. However, I think you might just need to change the code I gave you to
if(getOneIntersectingObject(shot.class))  
{  
          explode();  
          return;
}
I'm only guessing though, since I'm not sure what the error is.
MatheMagician MatheMagician

2012/7/11

#
Oh, but thanks for posting the screenshot, it helped a lot.
SPower SPower

2012/7/11

#
Some tips for KommandoFaust: class names start with a an uppercase, so this:
shot shot = new shot();
would look like this:
Shot shot = new Shot();
Java won't complain if you don't do this, but you do confuse other programmers. And about this:
KommandoFaust wrote...
... plane to explode with "void explode()"...
you don't have to say the return type when you're talking about a method :)
KommandoFaust KommandoFaust

2012/7/11

#
But thank you for your kind help, im a newbie so all what you say is good for me :-) Got an error incompatible types with - (shot.class) I tired it with "shot" and "Shot.class" but it seems thats not the problem or?
SPower SPower

2012/7/11

#
Which line?
KommandoFaust KommandoFaust

2012/7/11

#
I probably got that sloved now, but, I have still an error - "reached end of file by parsing"
KommandoFaust KommandoFaust

2012/7/11

#
Wanna take a look? Here is the whole code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class p2e here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class p2e extends Actor
{
    private static final int NUM_FRAGMENTS = 1;
    /**
     * Act - do whatever the p2e wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    public void explode()
    {
        placep2edes (getX(), (getY()), NUM_FRAGMENTS);
        getWorld().removeObject(this);
}

private void placep2edes(int x, int y, int numfragments)
{
        if(getOneIntersectingObject(shot.class))    
    {    
              explode();    
              return;  
    }  
        getWorld().addObject (new p2edes(), x, y );
    }
}  
KommandoFaust KommandoFaust

2012/7/11

#
The error is in this case
      if(getOneIntersectingObject(shot.class))    
There are more replies on the next page.
1
2
3
4