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

2013/12/15

getting my platforms unaccessable

virginia001 virginia001

2013/12/15

#
i have a question......i want to make static platforms that are unaccesable by mij ships. so far i have tried some code but nothing works... can anybody plz help me with this.
danpost danpost

2013/12/16

#
I think you need to be more specific as to what you want. Plus, it would help if you show what code you have tried (giving appropriate background information).
virginia001 virginia001

2013/12/16

#
so far i have this... public class Schipgroen extends Schip { /** * Act - do whatever the Schipgroen wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int speed = 3; public void act() { if(Greenfoot.isKeyDown("left")) { moveLeft(); } if(Greenfoot.isKeyDown("right")) { moveRight(); } if(Greenfoot.isKeyDown("up")) { moveUp(); } if(Greenfoot.isKeyDown("down")) { moveDown(); } } public void moveRight() { setLocation(getX() + speed, getY()); setRotation(180); } public void moveLeft() { setLocation(getX() - speed, getY()); setRotation(0); } public void moveUp() { setLocation(getX(), getY() - speed); setRotation(90); } public void moveDown() { setLocation(getX(), getY() + speed); setRotation(270); } public boolean Platform() { Actor a = getOneObjectAtOffset(283, 729, Platform.class); if(a == null) { speed =2; } else { speed =0; } } }
danpost danpost

2013/12/16

#
This:
getOneObjectAtOffset(283, 729, Platform.class);
will look for Platform objects that are 283 cells to the right and 729 cells below your Schipgroen object (the values are offsets from 'this.getX()' and 'this.getY()'; not world coordinates).
virginia001 virginia001

2013/12/16

#
so if want to stop my ship from moving when it collides against all of my platforms objects that are locating at the below code, what do you suggest? platform.setLocation(283, 729); platform2.setLocation(355, 729); platform3.setLocation(427, 729); platform5.setLocation(499, 729); platform8.setLocation(283, 657); platform4.setLocation(355, 657); platform7.setLocation(427, 657); platform6.setLocation(499, 657); platform9.setLocation(283, 585); platform10.setLocation(355, 585); platform11.setLocation(427, 585); platform12.setLocation(499, 585);
danpost danpost

2013/12/16

#
When calling each of your movement methods (moveLeft, moveRight, etc), do something like this:
moveLeft();
setRotation(180);
if (getOneIntersectingObject(Platfom.class) != null) moveRight();
The above was only for one direction. Put the rotation setting like above and not in the move methods themselves.
virginia001 virginia001

2013/12/16

#
Thanks bro it worked! i have another question if you don't mind...... when my ship is is going left,up or down it perfectly rotates the way it supposed to, but when it goes to the right my ship flips upside down..
danpost danpost

2013/12/17

#
With what you have, it appears you will have to set the image within each of the move methods and then in the moveRight method, also mirror the image vertically (refer to the GreenfootImage documentation).
virginia001 virginia001

2013/12/17

#
okay i have what i want partially... it rotates with the direction you go but it keeps flipping like crazy.. if(Greenfoot.isKeyDown("left")) { moveLeft(); getImage().mirrorVertically(); } if(Greenfoot.isKeyDown("right")) { moveRight(); getImage().mirrorVertically(); } if(Greenfoot.isKeyDown("up")) { moveUp(); getImage().mirrorHorizontally(); } if(Greenfoot.isKeyDown("down")) { moveDown(); getImage().mirrorHorizontally(); }
danpost danpost

2013/12/17

#
I guess you misunderstood. 'mirrorVertically' was only to be used with 'moveRight'. 'setImage' needs to be used at the beginning of all four direction checking blocks.
virginia001 virginia001

2013/12/17

#
Okay, i have adjusted my code into this but keep giving me the error: no suitable method found for setImage() public void act() { if(Greenfoot.isKeyDown("left")) { setImage(); moveLeft(); } if(Greenfoot.isKeyDown("right")) { moveRight(); getImage().mirrorVertically(); } if(Greenfoot.isKeyDown("up")) { setImage(); moveUp(); } if(Greenfoot.isKeyDown("down")) { setImage(); moveDown(); } }
danpost danpost

2013/12/17

#
You need to supply the name of the file (as a String) in the 'setImage' call.
setImage(/* image filename */);
This also needs to be used before the call to 'moveRight'.
virginia001 virginia001

2013/12/17

#
Thanks! it works..
You need to login to post a reply.