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

2013/3/30

method addObject(greenfoot.Actor,int,int) cannot be referenced from a static context.

BubbaB BubbaB

2013/3/30

#
So basically I'm just trying to create an instance of the Bullet class from my Rocket class at position 200. 200. Any help would be appreciated. Here is my whole Rocket class and I can post my bullet class too if you want. Thanks in advanced! import greenfoot.*; /** * Write a description of class Rocket here. * * @author (your name) * @version (a version number or a date) */ public class Rocket extends Actor { int xpos = 303; /** * Act - do whatever the Rocket wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("left")) { xpos += -4; } if(Greenfoot.isKeyDown("right")) { xpos += 4; } if(Greenfoot.isKeyDown("space")) { Bullet bullet = new Bullet(); World.addObject(bullet, 200, 200); } setLocation(xpos, getY()); } public Rocket() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 40, image.getHeight() - 20); setImage(image); setRotation(-90); } }
danpost danpost

2013/3/30

#
The line: World.addObject(bullet, 200, 200); is telling trying to add the bullet to a class ('World'), but you need to add the bullet to an instance of that class (a world object). An actor (like the Rocket object) can use its 'getWorld' method to get the world instance that it is in. Use this instead: getWorld().addObject(bullet, 200, 200);
BubbaB BubbaB

2013/3/30

#
Alright thank you it worked!
You need to login to post a reply.