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

2013/5/15

help it says non -static method setSpeed(int) cannot be reference from a static context

sergiodeleon702 sergiodeleon702

2013/5/15

#
public class Paddle2 extends Actor { private boolean mouseKeyDown = false; public void act() { if (!mouseKeyDown && Greenfoot.mousePressed(null)) { mouseKeyDown = true; } else if (mouseKeyDown && Greenfoot.mouseClicked(null)) { mouseKeyDown = false; } if (mouseKeyDown) { MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse != null) { setLocation(mouse.getX(), mouse.getY()); } } Actor puck = getOneIntersectingObject(Puck.class); if (puck != null) { Puck.setSpeed(7); } } }
Gevater_Tod4711 Gevater_Tod4711

2013/5/15

#
You are trying to access the puck using the classname (Puck) but that's only possible if the method is static. For non static methods you have to use the reference name (puck in this case).
sergiodeleon702 sergiodeleon702

2013/5/15

#
so what would i have to change? im stuck on this
Gevater_Tod4711 Gevater_Tod4711

2013/5/15

#
Just change Puck.setSpeed(7); to puck.setSpeed(7); (no capital letter).
sergiodeleon702 sergiodeleon702

2013/5/15

#
it says cannot find symbol variable puck
davmac davmac

2013/5/15

#
Gevater_Tod4711 wrote...
Just change Puck.setSpeed(7); to puck.setSpeed(7); (no capital letter).
This is partially true. You'll also need to do a cast to the correct type. Change this:
    Actor puck = getOneIntersectingObject(Puck.class);
    if (puck != null) {
        Puck.setSpeed(7);
    }
to this:
    Puck puck = (Puck) getOneIntersectingObject(Puck.class);
    if (puck != null) {
        puck.setSpeed(7);
    }
You need to login to post a reply.