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

2014/1/11

Moving platforms

coldxms coldxms

2014/1/11

#
Hello, i need some help with my platform class. I got 1 platform moving from left to right.. but i want another moving on the Y axis. However i've tried several approaches including DanPost's Method: "
1
2
3
4
5
6
7
public void moveplatformupdown(int Ya, int Yb){
    Actor flyer2 = (Actor) getWorld().getObjects(flyingplatform.class).get(1);
    Actor player = (Actor) getWorld().getObjects(Player.class).get(0);
    minimumY = Ya;
    maximumY = Yb;
    direction = 1;
}
}" Which he helped someone else with earlier. The platform moves up, but when reaching the point given in the method it just stops. it seems to be moving up and down at the same time! heres my full code of the class:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class flyingplatform here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class flyingplatform extends Platform
{
    private GreenfootImage platformleft = new GreenfootImage("platform.png");
    private GreenfootImage platformright = new GreenfootImage("platformright.png");
    private int minimumY;
    private int maximumY;
    private int direction;
    boolean gelijk = false;
    boolean moveUp = true;
    /**
     * Act - do whatever the flyingplatform wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        moveplayerleftright();
        moveplatformleftright();
        moveplatformupdown(300,400);
    }
    public void moveplayerleftright()
    {
        Actor player = (Actor) getWorld().getObjects(Player.class).get(0);
        Actor flyer1 = (Actor) getWorld().getObjects(flyingplatform.class).get(0);
         
        if((player.getX() == flyer1.getX() - 10 && player.getY() < 545 ) || (player.getX() == flyer1.getX() + 10 && player.getY() < 545 ))
        {
            gelijk = true;
        }
        if(gelijk)
        {
            player.setLocation(flyer1.getX(), flyer1.getY() - 25);
        }
        if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("z"))
            {
                gelijk = false;
            }
    }
    public void moveplatformleftright(){
        Actor flyer1a = (Actor) getWorld().getObjects(flyingplatform.class).get(0);
        if(flyer1a.getX() >= 320)
        {
            flyer1a.move(-1);
        }else if(flyer1a.getX() <= 320){
            flyer1a.setRotation(180);
            flyer1a.move(-1);
            flyer1a.setImage(platformright);
        }
        if(flyer1a.getX() >= 460 && flyer1a.getX() <= 466)
        {
            flyer1a.setRotation(0);
            flyer1a.move(-1);
            flyer1a.setImage(platformleft);
        }
    }
    public void moveplayerupdown()
    {
        Actor player = (Actor) getWorld().getObjects(Player.class).get(0);
        Actor flyer2 = (Actor) getWorld().getObjects(flyingplatform.class).get(1);
         
        if((player.getX() == flyer2.getX() - 10 && player.getY() < 545 ) || (player.getX() == flyer2.getX() + 10 && player.getY() < 545 ))
        {
            gelijk = true;
        }
        if(gelijk)
        {
            player.setLocation(flyer2.getX(), flyer2.getY() - 25);
        }
        if(Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("z"))
            {
                gelijk = false;
            }
    }
    public void moveplatformupdown(int Ya, int Yb){
        Actor flyer2 = (Actor) getWorld().getObjects(flyingplatform.class).get(1);
        Actor player = (Actor) getWorld().getObjects(Player.class).get(0);
        minimumY = Ya;
        maximumY = Yb;
        direction = 1;
         
        if(flyer2.getY() <= minimumY) direction = 1;
        if(flyer2.getY() >= maximumY) direction = -1;
        setLocation(flyer2.getX(), flyer2.getY() + direction);
    }
}
danpost danpost

2014/1/11

#
In which discussion did you acquire the code you mentioned above that I helped with.
coldxms coldxms

2014/1/11

#
http://www.greenfoot.org/topics/1443
danpost danpost

2014/1/11

#
Alright, that was over two years ago; plus I noted in that discussion that "this code is not tested". I have noticed, just glancing over it, that there are a few mistakes in the code, also. And I think it just got worse, you trying to implement and fix it. Lets us start from scratch. For a platform that can move either vertically or horizontally, you will need an instance boolean field to determine which axis it will move along and two instance int fields to determine the end points along that axis where the platform will change directions. We also need one more instance int field to determine the current direction of movement. Therefore we have this:
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
import greenfoot.*;
 
public class Platform extends Actor
{
    private boolean movesVertically; // 'false' = moves horizontally
    private int loCoord, hiCoord; // the turn-around coordinates
    private int direction = 1; // starts off moving right or down
    // the value of the above fields should be supplied when creating the platform
    public Platfom(boolean toMoveVertically, int loValue, int hiValue)
    {
        movesVertically = toMoveVertically;
        loCoord = loValue;
        hiCoord = hiValue;
    }
    // the act method for movement (ignoring player for now)
    public void act()
    {
        if (movesVertically)
        {
            setLocation(getX(), getY()+direction);
            if ( (direction < 0 && getY() < loCoord) || (direction > 0 && getY() > hiCoord) ) direction *= -1;
        }
        else
        {
            setLocation(getX()+direction, getY());
            if ( (direction < 0 && getX() < loCoord) || (direction > 0 && getX() > hiCoord) ) direction *= -1;
        }
    }
}
With this, you should be able to create a simple platform that will either move vertically or horizontally between two point along that axis. Create each platform using something like 'new Platform(false, 100, 400)'. The 'false' says not to move vertically, so move horizontally between the x's of 100 and 400. From here we can add the player and make it move with the platform. You will need an instance Player field to be null when the player is not on it and to refer to the player when it is on it. This will be controlled by the Player class, but used in the act of the Platform class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// another instance field
private Player player;
// revised act method
public void act()
{
    if (movesVertically)
    {
        setLocation(getX(), getY()+direction);
        if (player != null) player.setLocation(player.getX(), player.getY()+direction);
        if ( (direction < 0 && getY() < loCoord) || (direction > 0 && getY() > hiCoord) ) direction *= -1;
    }
    else
    {
        setLocation(getX()+direction, getY());
        if (player != null) player.setLocation(player.getX()+direction, player.getY());
        if ( (direction < 0 && getX() < loCoord) || (direction > 0 && getX() > hiCoord) ) direction *= -1;
    }
}
Now, we need a way for the Player to tell the platform that it is on it or not. So add the following method in the Platform class:
1
2
3
4
public void setPlayer(Player actor)
{
    player = actor;
}
The player now must be able to use that method; but it must also be able to set the value back to 'null' when no longer on the platform, so it would need to know which platform it told it was on. We need an instance Platform field in the Player class to track this. Anytime the player is about to move, it should do the following:
1
if (platform != null) platform.setPlayer(null);
then after the move
1
2
platform = (Platform)getOneObjectAtOffset(0, 20, Platform.class)
if (platform != null) platform.setPlayer(this);
The above line setting the value of 'platform' was an example and may not be exactly what you want; but the idea is to get a reference to the platform object that the player is on and call the method on it, telling it that the player is there (if a platform was there).
coldxms coldxms

2014/1/11

#
couple a questions on the above given code.. Q1: am creating a new platform in the world class.
1
flyingplatform flyer2 = new flyingplatform(true,300,400);
gives me the following error:
Constructor flyingplatform in class flying platform cannot be applied to given types; required: no arguments; found boolean, int,int; reason: actual and formal argument list differ in length
How to solve this? Q2; the
1
public Platfom(boolean toMoveVertically, int loValue, int hiValue)
asks for a return type. Must this be string? Q3: the
1
if (platform != null) platform.setPlayer(null);
should this be implemented in the players controls? E.G. i use left and right to move
1
2
3
4
5
6
7
8
if(Greenfoot.isKeyDown("right") && !Greenfoot.isKeyDown("up") && !Greenfoot.isKeyDown("down")){
        setRotation(0);
        setLocation(posx + 3, posy);
        direction = 1;
        if(animationCounter % 4 == 0){
        animateRight();
    }
    }
if u wish to see more of my scenario i'd be happy to share it if i know how!
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class flyingplatform here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class flyingplatform extends Platform
{
    /**private GreenfootImage platformleft = new GreenfootImage("platform.png");
    private GreenfootImage platformright = new GreenfootImage("platformright.png");
    private int minimumY;
    private int maximumY;
    boolean gelijk = false;
    */boolean moveUp = true;
    private boolean movesVertically; // 'false' = moves horizontally 
    private int loCoord, hiCoord; // the turn-around coordinates 
    private int direction = 1; // starts off moving right or down 
    // the value of the above fields should be supplied when creating the platform
    private Player player;
    public void platform(boolean toMoveVertically, int loValue, int hiValue) 
    
        movesVertically = toMoveVertically; 
        loCoord = loValue; 
        hiCoord = hiValue;
    }
    public void act() 
    
        if (movesVertically) 
        
            setLocation(getX(), getY()+direction); 
            if (player != null) player.setLocation(player.getX(), player.getY()+direction); 
            if ( (direction < 0 && getY() < loCoord) || (direction > 0 && getY() > hiCoord) ) direction *= -1
        
        else 
        
            setLocation(getX()+direction, getY()); 
            if (player != null) player.setLocation(player.getX()+direction, player.getY()); 
            if ( (direction < 0 && getX() < loCoord) || (direction > 0 && getX() > hiCoord) ) direction *= -1
        
    }
    public void setPlayer(Player actor) 
    
        player = actor; 
    
}
danpost danpost

2014/1/12

#
Alright, let me ask you this: how many different types of platforms are you using (or, in other words, what are the subclasses for Platform and how are they all different from each other)?
coldxms coldxms

2014/1/12

#
platform - straight - flyingplatform 1 must move the other musn't and for now i think a max of 10 platforms. that wil move divided over 3 worlds
danpost danpost

2014/1/12

#
What I am trying to say is that you only need one platform class. It will be able to create some that move vertically, some that move horizontally and some that stand still as well. You do not need to sub-class the Platform class. All 10 of your platforms can be created from the one Platform class. To include some that can stand still, we just need a way to change the value of the 'direction' field to zero. Add the following constructor to my Platform class code above (while keeping the other one):
1
2
3
4
public Platform()
{
    direction = 0;
}
Now simply use 'new Platform()' to create a standing platform and 'new Platform(boolean, int, int) to create one that moves.
danpost danpost

2014/1/12

#
We could even have platforms change from standing still to moving or even change the axis of movement without having to remove a platform of one class and replacing it with a platform of another class. Only thing needed would be to add a few simple methods. Maybe one to 'inactivate' (make stand still), one to 'activate' (start moving) and one to 'setPath(boolean, int, int)' (set or change the direction or range of movement).
coldxms coldxms

2014/1/12

#
Keeping them in motion works for me. How ever i wish to keep the 2 subclasses as it makes it easier for me to understand. What i did not explain is that the Platform makes the actor able to walk on it and not through it. So you could consider it as a wall and floor. So all subclasses inherit that. The Platform has an image of the floor i use for the ground of my game. Straight has an image i use for 'blocks" to walk on. and flying platform has one that i use to animate it. The problem i now encounter is that my player will not stay on the platform.
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
public void characterController(){
String key = Greenfoot.getKey();
    int posx = getX();
    int posy = getY();
    if (platform != null) platform.setPlayer(null);
if("z".equals(key) && jumping == false)
{
    jump();
}
 
//East//
if(Greenfoot.isKeyDown("right") && !Greenfoot.isKeyDown("up") && !Greenfoot.isKeyDown("down")){
        setRotation(0);
        setLocation(posx + 3, posy);
        direction = 1;
        if(animationCounter % 4 == 0){
        animateRight();
    }
    }
 
//West//
if(Greenfoot.isKeyDown("left") && !Greenfoot.isKeyDown("down") && !Greenfoot.isKeyDown("up")){
        setRotation(180);
        setLocation(posx - 3, posy);
        direction = -1;
        if(animationCounter % 4 == 0){
        animateLeft();
    }
    }
 
platform = (flyingplatform)getOneObjectAtOffset(0, 20, flyingplatform.class) ;
if (platform != null) platform.setPlayer(this);
This is my Player.class where it moves
danpost danpost

2014/1/12

#
Then maybe you should be showing your Platform class code (since it controls the player on it and the others you say inherit that)
coldxms coldxms

2014/1/12

#
The Platform is a superclass used for all subclasses to not walk through it. The actual check if there are platforms or subclasses of platforms (straight and flyingplatform) are in the Player.class.
1
2
3
4
5
6
7
8
public void moveToGround(Actor ground)
{
    int groundHeight = ground.getImage().getHeight();
    int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
     
    setLocation(getX(), newY);
    jumping = false;
}
The following code is my Whole player class: in the actual platform class is no code apart from my attempt to change an image when it was on a position (which i no longer use)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Player here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    private int vSpeed = 0;
    private int vSpeed2 = 0;
    private int acceleration = 2;
    private int acceleration2 = 2;
    private boolean jumping;
    private boolean jumping2;
    private int jumpStrength = 20;
    private int jumpStrength2 = 10;
    private GreenfootImage run1r = new GreenfootImage("Mario-1.png");
    private GreenfootImage run2r = new GreenfootImage("Mario-2.png");
    private GreenfootImage run3r = new GreenfootImage("Mario-3.png");
    private GreenfootImage run4r = new GreenfootImage("Mario-4.png");
    private GreenfootImage run5r = new GreenfootImage("mario-5.png");
    private GreenfootImage run6r = new GreenfootImage("mario-6.png");
    private GreenfootImage run7r = new GreenfootImage("mario-7.png");
    private GreenfootImage run8r = new GreenfootImage("mario-8.png");
    private int frame = 1;
    private int animationCounter = 0;
    private GreenfootImage run1l = new GreenfootImage("Mario-1l.png");
    private GreenfootImage run2l = new GreenfootImage("Mario-2l.png");
    private GreenfootImage run3l = new GreenfootImage("Mario-3l.png");
    private GreenfootImage run4l = new GreenfootImage("Mario-4l.png");
    private GreenfootImage run5l = new GreenfootImage("mario-5l.png");
    private GreenfootImage run6l = new GreenfootImage("mario-6l.png");
    private GreenfootImage run7l = new GreenfootImage("mario-7l.png");
    private GreenfootImage run8l = new GreenfootImage("mario-8l.png");
    private int framel = 1;
    private int direction = 1;
    private int shootingCounterleft = 100;
    private int shootingCounterright = 100;
    private flyingplatform platform;
 
    public void act()
    {
        checkFall();
        characterController();
        animationCounter++;
        platformAbove();
        checkRightWalls();
        checkLeftWalls();
        eatPizza();
        shootingCounterleft --;
        shootingCounterright --;
        shooting();
        checkfire();
    }   
    public boolean shooting()
    {
        if(Greenfoot.isKeyDown("s") && shootingCounterleft <= 0 && direction == 1){
            getWorld().addObject(new Shootright(), getX(), getY());
            shootingCounterleft = 100;
            return true;
        }
        if(Greenfoot.isKeyDown("s") && shootingCounterright <= 0 && direction == -1){
            getWorld().addObject(new Shootleft(), getX(), getY());
            shootingCounterright = 100;
            return true;
        }
        return false;
    }
 
        public void characterController(){
        String key = Greenfoot.getKey();
            int posx = getX();
            int posy = getY();
            if (platform != null) platform.setPlayer(null);
        if("z".equals(key) && jumping == false)
        {
            jump();
        }
         
        //East//
        if(Greenfoot.isKeyDown("right") && !Greenfoot.isKeyDown("up") && !Greenfoot.isKeyDown("down")){
                setRotation(0);
                setLocation(posx + 3, posy);
                direction = 1;
                if(animationCounter % 4 == 0){
                animateRight();
            }
            }
         
        //West//
        if(Greenfoot.isKeyDown("left") && !Greenfoot.isKeyDown("down") && !Greenfoot.isKeyDown("up")){
                setRotation(180);
                setLocation(posx - 3, posy);
                direction = -1;
                if(animationCounter % 4 == 0){
                animateLeft();
            }
            }
         
        platform = (flyingplatform)getOneObjectAtOffset(0, 20, flyingplatform.class) ;
        if (platform != null) platform.setPlayer(this); 
         
        }
        public void animateLeft()
        {
            if(framel == 1)
            {
                setImage(run1l);
            }
            else if(framel == 2)
            {
                setImage(run2l);
            }
            else if(framel == 3)
            {
                setImage(run3l);
            }
            else if(framel == 4)
            {
                setImage(run4l);
            }
            else if(framel == 5)
            {
                setImage(run5l);
            }
            else if(framel == 6)
            {
                setImage(run6l);
            }
            else if(framel == 7)
            {
                setImage(run7l);
            }
            else if(framel == 8)
            {
                setImage(run8l);
                framel = 1;
                return;
            }
            framel++;
        }
        public void animateRight()
        {
            if(frame == 1)
            {
                setImage(run1r);
            }
            else if(frame == 2)
            {
                setImage(run2r);
            }
            else if(frame == 3)
            {
                setImage(run3r);
            }
            else if(frame == 4)
            {
                setImage(run4r);
            }
            else if(frame == 5)
            {
                setImage(run5r);
            }
            else if(frame == 6)
            {
                setImage(run6r);
            }
            else if(frame == 7)
            {
                setImage(run7r);
            }
            else if(frame == 8)
            {
                setImage(run8r);
                frame = 1;
                return;
            }
            frame++;
        }
        public void fall()
        {
            setLocation(getX(), getY() +vSpeed);
            if(vSpeed <= 9)
            {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
        }
        //check if on ground
        public boolean onGround()
        {
            int spriteHeight = getImage().getHeight();
            int lookForGround = (int)(spriteHeight/2);
             
            Actor ground = getOneObjectAtOffset(0, lookForGround, Platform.class);
            if(ground == null)
            {
                jumping = true;
                return false;
            }
            else
            {
                moveToGround(ground);
                return true;
            }
        }
         
         
         public boolean platformAbove()
        {
            int spriteHeight = getImage().getHeight();
            int yDistance = (int)(spriteHeight/-2);
             
            Actor ceiling = getOneObjectAtOffset(0, yDistance, Platform.class);
             
            if(ceiling != null)
            {
                vSpeed = 1;
                bopHead(ceiling);
                return true;
            }
            else
            {
                 
                return false;
            }
        }
         
        public boolean checkRightWalls()
        {
            int spriteWidth = getImage().getWidth();
            int xDistance = (int)(spriteWidth/2);
             
            Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
             
            if(rightWall == null)
            {
                return false;
            }
            else
            {
                stopByRightWall(rightWall);
                return true;
            }
        }
        public void stopByRightWall(Actor rightWall)
        {
            int wallWidth = rightWall.getImage().getWidth();
            int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2;
            setLocation(newX - 5, getY());
        }
        public boolean checkLeftWalls()
        {
            int spriteWidth = getImage().getWidth();
            int xDistance = (int)(spriteWidth/ 2);
             
            Actor leftWall = getOneObjectAtOffset(-xDistance, 0, Platform.class);
             
            if(leftWall == null)
            {
                return false;
            }
            else
            {
                stopByLeftWall(leftWall);
                return true;
            }
        }
        public void stopByLeftWall(Actor leftWall)
        {
            int wallWidth = leftWall.getImage().getWidth();
            int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2;
            setLocation(newX + 5, getY());
        }
        public void bopHead(Actor ceiling)
        {
            int ceilingHeight = ceiling.getImage().getHeight();
            int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
             
            setLocation(getX(), newY);
        }
         
        public void moveToGround(Actor ground)
        {
            int groundHeight = ground.getImage().getHeight();
            int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
             
            setLocation(getX(), newY);
            jumping = false;
        }
 
        public void checkFall()
        {
            if(onGround())
            {
                vSpeed = 0;
            }
            else
            {
                fall();
            }
        }
         
        public void jump()
        {
            vSpeed = vSpeed - jumpStrength;
            jumping = true;
            fall();
        }
        public void eatPizza()
        {
            Actor pizza;
            pizza = getOneObjectAtOffset(0,0,Pizza.class);
            if( pizza != null)
            {
                World haven;
                haven = getWorld();
                haven.removeObject(pizza);
            }
        }
         
        public void checkfire(){
            Actor fire = getOneObjectAtOffset(0,0,Fire.class);
            if(fire != null)
            {
                setLocation(20,697);
            }
        }
     
    
}
coldxms coldxms

2014/1/14

#
Anyone?
You need to login to post a reply.