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

2012/10/26

Help

1
2
Tezuka Tezuka

2012/10/26

#
Hi I need help. When i shoot out a cannonball it doesnt change direction after the cannon here is my cannon class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cannon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cannon extends Actor
{
    int power = 100;
    int angle = 0;
    private StatusBoard statusboard = null;
    /**
     * Act - do whatever the Cannon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    

    
    
    public void act() 
    {   
         if(Greenfoot.isKeyDown("up"))
        {
            power = +1;
        }
        
         if(Greenfoot.isKeyDown("down"))
        {
            power = -1;
        }
        
        if(Greenfoot.isKeyDown("right") && angle > 0 )
        {
            angle = angle - 1;
        }
        
        if(Greenfoot.isKeyDown("left") && angle < 90)
        {
            angle = angle + 1;
        }
         
        setRotation(-angle);
        
        
        if("space".equals(Greenfoot.getKey()))
        {
         fire();
        }
     }
     
     public void fire()
    {
         World myWorld = getWorld();
         Chicken chicken = new Chicken(angle,power);
         myWorld.addObject(chicken, getX(),getY());
    }
    
}
and here os my cannon ball class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Chicken here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Chicken extends Actor
{
    int GRAVITY = 2;
    int xSpeed = 0;
    int ySpeed = 0;
    /**
     * Act - do whatever the Chicken wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Chicken( int angle, int power)
    {
        power = (power * 10/ 25);
        xSpeed = (int)(power*Math.cos(Math.toRadians(angle)));
        ySpeed = (int)(power*Math.sin(Math.toRadians(angle)));
    }
    
    public void act() 
    {
        setLocation(getX()+ xSpeed, getY() -ySpeed);
        ySpeed = ySpeed = GRAVITY;
        
        World myWorld = getWorld();
        if(getY() > myWorld.getHeight())
        {
            myWorld.removeObject(this);
        }
    }    
}
danpost danpost

2012/10/26

#
Check out line 28 of the Chicken class; look at it closely (I do not think that is what you wanted).
Tezuka Tezuka

2012/10/26

#
thnx:D what a misstake
Tezuka Tezuka

2012/10/26

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

/**
 * Write a description of class Cannon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cannon extends Actor
{
    int power = 100;
    int angle = 0;
    private StatusBoard statusboard = null;
    /**
     * Act - do whatever the Cannon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public Cannon ( StatusBoard status)
    {
        statusBoard = status;
    }
    
    
    public void act() 
    {   
         if(Greenfoot.isKeyDown("up"))
        {
            power = +1;
        }
        
         if(Greenfoot.isKeyDown("down"))
        {
            power = -1;
        }
        
        if(Greenfoot.isKeyDown("right") && angle > 0 )
        {
            angle = angle - 1;
        }
        
        if(Greenfoot.isKeyDown("left") && angle < 90)
        {
            angle = angle + 1;
        }
         
        setRotation(-angle);
        statusBoard.setValues(angle,power);
        
        if("space".equals(Greenfoot.getKey()))
        {
         fire();
        }
     }
     
     public void fire()
    {
         World myWorld = getWorld();
         Chicken chicken = new Chicken(angle,power);
         myWorld.addObject(chicken, getX(),getY());
    }
}
I tried to add a scoreboard but cant compile it help pls
danpost danpost

2012/10/26

#
Can you see what is wrong with line 21? NVM, you fixed it.
Tezuka Tezuka

2012/10/26

#
nvm i got
Tezuka Tezuka

2012/10/26

#
when I shot the cannon, the cannonball appers in the middle of my cannon but I want it to appers at the end of the cannon is there any way to di it?
danpost danpost

2012/10/26

#
Yes. how about adding the following
// in the Chicken class constructor
setRotation(-angle);
// new method
public void addedToWorld(World world)
{
    move(40); // adjust the value to barrel length
}
Tezuka Tezuka

2012/10/26

#
yea it worked with move() but cant I use the setLocation?
Tezuka Tezuka

2012/10/26

#
I used thos method to remove the object but it didnt work, any ideas? if (getX() == myWorld. getWidth()) { myWorld.removeObject(this); } if(getY()== 0) { myWorld.removeObject(this); }
danpost danpost

2012/10/26

#
Tezuka wrote...
yea it worked with move() but cant I use the setLocation?
You could, but then you would have to do all the calculating as well.
Tezuka wrote...
if (getX() == myWorld. getWidth())
This will never be true. The highest x location an object can be is 'getWorld().getWidth() - 1'.
Tezuka Tezuka

2012/10/27

#
Hi Dan if I want a force to affect my cannon like wind for example whats iam suppose to begin with?
danpost danpost

2012/10/27

#
I believe you mean 'affect my shot (chicken)' (not cannon). You will need another instance variables (or field) in the Chicken class to hold the current wind speed. It will affect the speed in the x direction just like gravity affect the speed in the y direction.
Tezuka Tezuka

2012/10/27

#
Could u explain this lines for me? power = (power * 10)/ 25; xSpeed = (int)(power*Math.cos(Math.toRadians(angle))); ySpeed = (int)(power*Math.sin(Math.toRadians(angle))); setRotation(-angle);
danpost danpost

2012/10/27

#
The first line just applied a factor to the 'power' value, which only changes the relational speed (compared to other objects) of the object. NOTE: I only applied it because you were applying it originally. The next two lines calculate the change in the x and y directions each act() cycle, determined by the 'power' (speed) and 'angle' (vector).
There are more replies on the next page.
1
2