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)
1
2
3
4
5
6
7
8
9
10
11
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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:
1
2
3
4
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:
1
2
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:
1
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