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

2017/4/21

Shooting Bullet Where Actor Is Looking

1
2
Ostarius Ostarius

2017/4/21

#
Hello, I have an actor called "DonkeyKong" and he shoots bullets from a class named "Weapons" DonkeKong has 3 Images, left, right and up How do i make it that if he looks left the bullets go left? DonkeyKong Class (Shooting Bullet Part)
if (!spaceDown && Greenfoot.isKeyDown("space"))
        {
            spaceDown = true;
            Weapons bullet = new Weapons();
            getWorld().addObject(bullet, getX() + 60, getY() - 15);
        }
        
        if (spaceDown && !Greenfoot.isKeyDown("space"))
        {
            spaceDown = false;
        }
Weapons Class
public void act() 
    {
        this.setLocation(this.getX() + 5, this.getY());
        this.touchingEnemy();
    }    
    
    public void touchingEnemy()
    {
       Actor ast = getOneIntersectingObject(Enemies.class);
       if(ast != null) {
       if(this.isTouching(Enemies.class)) {    
            getWorld().removeObject(ast);
            getWorld().removeObject(this);
       }
       
       }
       else if(this.atWorldEdge())
       {
            this.getWorld().removeObject(this);
       } 
    }       
danpost danpost

2017/4/21

#
Ostarius wrote...
I have an actor called "DonkeyKong" and he shoots bullets from a class named "Weapons" DonkeKong has 3 Images, left, right and up How do i make it that if he looks left the bullets go left?
Need to see movement code and code changing the image of the actor (just post the entire class; it is easier and more information to aide in helping).
Ostarius Ostarius

2017/4/21

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

/**
 * Write a description of class DonkeyKong here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DonkeyKong extends Animal
{
    /**
     * Act - do whatever the DonkeyKong wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private int speed = 5;
    private int vSpeed = 5;
    private boolean spaceDown; 
    private int timer = 0;
    public static int score = 0;
    private GreenfootImage left = new GreenfootImage("DKleft.png");
    private GreenfootImage right = new GreenfootImage("DKright.png");
    private GreenfootImage up = new GreenfootImage("DKjumping.png");


    public void act() 
    {
        this.controls();
        this.moveVertically();
        //this.onGround();
        this.setLocation(getX(), getY()+vSpeed);
        this.touchingBricks();
        this.score();
        this.brickFix();
        //if (getOneIntersectingObject(Bricks.class) != null) move(-speed);         
        //if (getOneIntersectingObject(Bricks.class) != null) move(+speed); 
        
        //if (getOneIntersectingObject(Blocks.class) != null) move(-speed);         
        //if (getOneIntersectingObject(Blocks.class) != null) move(+speed);
    }    
    
    public void controls()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            this.setImage(right);
            this.move(speed);
        }
        
        if (Greenfoot.isKeyDown("left"))
        {
            this.setImage(left);
            this.move(-speed);
        }
        
        if (!spaceDown && Greenfoot.isKeyDown("space"))
        {
            spaceDown = true;
            Weapons bullet = new Weapons();
            getWorld().addObject(bullet, getX() + 60, getY() - 15);
        }
        
        if (spaceDown && !Greenfoot.isKeyDown("space"))
        {
            spaceDown = false;
        }
        
    }
    
    public boolean onGround()  
    {  
        int height = getImage().getHeight();  
        Actor ground = getOneObjectAtOffset(0, height/2, Bricks.class);  
        return ground != null;   
    }  
    
    public void touchingBricks()
    {
        
        if (!getIntersectingObjects(Bricks.class).isEmpty()) 
        {
            this.setLocation(getX(), getY()-5);
            //this.setImage("DKright.png");
        } 
 
    }

    public void moveVertically()
    {
        
        if (Greenfoot.isKeyDown("up"))
        {
            this.setImage(up);
            this.setLocation(getX(), getY()-25);
        }  
    }
    
    public void score()
    {
        if (this.isTouching(Blocks.class))
        {
            this.score++;
        }
    }
    
    public void brickFix()
    {
        if (isTouching(Bricks.class) && (getImage().equals(right)))
        {
            move(-speed);
        } 
        
        if (isTouching(Bricks.class) && (getImage().equals(left)))
        {
            move(speed);
        }    
        
        if (isTouching(Blocks.class) && (getImage().equals(right)))
        {
            move(-speed);
        } 
        
        if (isTouching(Blocks.class) && (getImage().equals(left)))
        {
            move(speed);
        }    
        
        if (isTouching(Blocks.class) && (getImage().equals(up)))
        {
            this.setLocation(getX(), getY()+20);
        }  
    }
    
}
Ostarius Ostarius

2017/4/21

#
And another Question. You see that i have the key "up" for jumping, how do i make it so it jumps only once? Like my bullets
danpost danpost

2017/4/21

#
First, add the following to the code:
public DonkeyKong()
{
    this.setImage(right);
}
This ensures that one of the three images is set to the actor. Then, insert the following at line 59:
if (getImage() == left) bullet.setRotation(180);
else if (getImage() == up) bullet.setRotation(270);
Because the image of the actor will be one of the three, you can use '==' instead of the 'equals' method. If is not the same for world background images, where using '==' on them will always return a 'false' value.
danpost danpost

2017/4/21

#
Ostarius wrote...
You see that i have the key "up" for jumping, how do i make it so it jumps only once? Like my bullets
You only want to jump when on the ground. Change line 90 to:
if (onGround() && Greenfoot.isKeyDown("up"))
Ostarius Ostarius

2017/4/21

#
Did both and both are not working :/
Ostarius Ostarius

2017/4/21

#
Possible Solution for the Shooting: In my Weapons class i wrote that it should go X()+5, what if we said if image is left then go -5? And i can't jump at all with the new code
danpost danpost

2017/4/21

#
Ostarius wrote...
In my Weapons class i wrote that it should go X()+5, what if we said if image is left then go -5?
In Weapons class, use 'move(5);' instead of 'setLocation(getX()+5, getY());'
Ostarius Ostarius

2017/4/21

#
danpost wrote...
Ostarius wrote...
In my Weapons class i wrote that it should go X()+5, what if we said if image is left then go -5?
In Weapons class, use 'move(5);' instead of 'setLocation(getX()+5, getY());'
Thanks! That works!
Ostarius Ostarius

2017/4/21

#
Ostarius wrote...
Did both and both are not working :/
Jumping is still not working :(
Ostarius Ostarius

2017/4/21

#
And sir, how can i make it that when i get to the end of the screen i can switch worlds?
danpost danpost

2017/4/21

#
Ostarius wrote...
i can't jump at all with the new code
In line 72, add '5' to 'height/2'.
Ostarius Ostarius

2017/4/21

#
danpost wrote...
Ostarius wrote...
i can't jump at all with the new code
In line 72, add '5' to 'height/2'.
Not Working
Ostarius Ostarius

2017/4/21

#
Ostarius wrote...
And sir, how can i make it that when i get to the end of the screen i can switch worlds?
No comment?
There are more replies on the next page.
1
2