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

2020/3/26

Why can't I shoot left even when the character turns left?

Shrace Shrace

2020/3/26

#
my character is meant to shoot both left and right. i have successfully created it to shoot right, but even when the character turns left he doesn't shoot left. the bubbles (bullet) keep shooting right, even as he moves left. Pleas help someone.. it for my AP exams and its due very soon.
Shrace Shrace

2020/3/26

#
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
public class avatar extends Actor
{
    private int speed = 6;
    private int verSpeed = 0;
    private int increase = 2;
    private int jumpstrength = 12;
    private boolean jumping;
    boolean touchingenemy = false;
    boolean touchingfood = false;
    boolean formula = false;
    int count = 0;
     
     
    /**
     * Act - do whatever the avatar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        checkKeys();
        checkfall();
        ceiling();
        eatfood();
        hitenemy();
        Fire();
             
    }
     
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("left"))
        {
           setImage("spongeleft.png");          
           moveLeft();
        }
        if (Greenfoot.isKeyDown("right"))
        {
           setImage("spongeright.png");
           moveRight();
        }
        if (Greenfoot.isKeyDown("up"))
        {
           jump();
        }
    }
     
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY());
    }
     
    public void moveRight()
    {
        setLocation ( getX() + speed, getY());
    }
     
    public void fall()
    {
        setLocation ( getX(), getY() + verSpeed);
        verSpeed = verSpeed + increase;
        jumping = true;
    }
     
    public void jump()
    {
        verSpeed = - jumpstrength;
        jumping = true;
        fall();
    }
     
    public void movetoground(Actor log)
    {
        int logHeight = log.getImage().getHeight();
        int newY = log.getY() - (logHeight +getImage().getHeight())/2;
         
        setLocation(getX(), newY);
         
    }
     
    public void checkfall()
    {
        if(onground()) {
            verSpeed = 0;
        }
        else{
            fall();
        }
         
    }
     
    public boolean onground()
    {
        int avatarHeight = getImage().getHeight();
        int lookforground = (int) (avatarHeight/2) + 5;
         
        Actor ground = getOneObjectAtOffset ( 0, getImage().getHeight()/2, log.class);
        if(ground == null){
            jumping = true;
            return false;
        }
        else{
            movetoground(ground);
            return true;
        }
          
     
        
    }
     
    public boolean ceiling()
    {
        int avatarHeight = getImage().getHeight();
        int Ydistance = (int) (avatarHeight / -2);
         
        Actor ceiling = getOneObjectAtOffset ( 0, Ydistance, log.class);
        if(ceiling != null){
             
            verSpeed =1;
            bopHead(ceiling);
            return true;
        }
        else{
            
            return false;
        }
         
    }
     
    public void bopHead(Actor ceiling)
    {
       int ceilingHeight = ceiling.getImage().getHeight();
       int newY = ceiling.getY() + (ceilingHeight +getImage().getHeight())/2;
         
        setLocation(getX(), newY);
         
    }
     
    public void hitenemy()
    {
        Actor enemy = getOneIntersectingObject(enemy.class);
        if (enemy != null)
        {
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            healthbar healthbar = myworld.gethealthbar();
            if(touchingenemy == false)
            {
               healthbar.losehealth();
               touchingenemy = true;
               if(healthbar.health<= 0)
                {
                   myWorld.removeObject(this);
                   gameover mygameover = new gameover();
                   Greenfoot.setWorld(mygameover);
                }
            }
        } else
        {
            touchingenemy = false;
        }
    }
     
    public void Fire()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            bubble bubbles = new bubble();
            World myWorld = getWorld();
            myWorld.addObject(new bubble(getRotation()), getX(), getY());           
            bubbles.setLocation(getX(), getY());
            bubbles.setRotation(getRotation());
           
        }
    }
     
    public void eatfood()
    {
        Actor food = getOneIntersectingObject(food.class);
        if (food != null)
        {
            World myWorld = getWorld();
            MyWorld myworld = (MyWorld)myWorld;
            healthbar healthbar = myworld.gethealthbar();
            if(touchingfood == false)
           {
             healthbar.gainhealth();
             touchingfood = true;
           }
        } else
        {
            touchingfood = false;
        }
    }
     
 
         
   }
danpost danpost

2020/3/27

#
Shrace wrote...
Why can't I shoot left even when the character turns left?
Because you are not turning the character to face left (you are just changing its image to one that faces left).
You need to login to post a reply.