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

2014/9/22

Hey my code has gone wrong I want my spaceship to shot in the direction its facing

coder04 coder04

2014/9/22

#
This is my code but it says 'can not find symbol - method getRotation ()' and I cant compile it import greenfoot.*; public class Miniship extends Actor { private int timer = 0; public void act() { move(4); fire(); reduceTime(); randomTurn(); turnAtEdge(); } public void fire() { if (timer == 0) { getWorld().addObject(new Enemylaser(), getX(), getY()); enemylaser.setRotation(getRoatation()); timer = 40; Greenfoot.playSound("laser.wav"); } } public void reduceTime() { if (timer > 0) timer--; } /** * With a 10% probability, turn a bit right or left. */ public void randomTurn() { if ( Greenfoot.getRandomNumber(100) < 10 ) { turn( Greenfoot.getRandomNumber(40)-20 ); } } /** * If we reach the edge of the world, turn a little bit. */ public void turnAtEdge() { if (atWorldEdge()) { turn(7); } } public boolean atWorldEdge() { if(getX() < 10 || getX() > getWorld().getWidth() - 10) return true; if(getY() < 10 || getY() > getWorld().getHeight() - 10) return true; else return false; } }
lordhershey lordhershey

2014/9/22

#
you have a mis-spelling: getRoatation(); should be: getRotation();
coder04 coder04

2014/9/22

#
now it says .setRotation is 'non-static method setRotation(int) cannot be referenced from a static context
danpost danpost

2014/9/22

#
You cannot call a non-static method on a class. You must call it on an object of that class. 'setRotation' is a non-static method in the Actor class. It must be called on an Actor object created from the class, not on the class itself. When you create an object using 'new ActorClassName()', you can assign it to a field that holds an Actor:
Actor actor = new ActorClassName();
BTW, not saying you should be using the above line in your case; but maybe something like it is what you need. Then, you can use 'setRotation' on 'actor', which holds an Actor object; but you cannot use it on 'ActorClassName' because it is a class name, not an Actor object. Classes do not have a rotation to set; but Actor objects do.
danpost danpost

2014/9/23

#
Maybe I am wrong about you using it on a class name. I noticed that the class name was 'Enemylaser' and you are calling 'setRotation' on 'enemylaser', which kind of surprised me. That is because I would have expected a message more like 'cannot find symbol enemylaser' instead of 'cannot be referenced from a static context'. However, the above was related to the message you said you were getting.
Super_Hippo Super_Hippo

2014/9/23

#
Are you sure that his class name started with a capital letter? If his class is called 'enemylaser', then this would explain the confusion.
You need to login to post a reply.