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

2021/6/15

I need help making a grabbing mechanic

1
2
eboy600 eboy600

2021/6/15

#
I am making a fishing game where you fish up plastic from the water, can anyone help, i want the hook to pick up the plastic when they touch. I have no clue what I'm doing.
Roshan123 Roshan123

2021/6/15

#
//global
Hook hook = new Hook();

//Act method
if(isTouching(Hook.class))
setLocation(hook.getX(), hook.getY());
If u don't like the above one then Plz wait to get some better mechanisms
eboy600 eboy600

2021/6/15

#
Roshan123 wrote...
//global
Hook hook = new Hook();

//Act method
if(isTouching(Hook.class))
setLocation(hook.getX(), hook.getY());
If u don't like the above one then Plz wait to get some better mechanisms
i put this code into my game but it came up with this error when they touch java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:713) at greenfoot.Actor.getX(Actor.java:165) at Bottle.act(Bottle.java:30) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183)
Roshan123 Roshan123

2021/6/15

#
eboy600 wrote...
i put this code into my game but it came up with this error when they touch
Is thier any remove statement? If yes then plz execute the remove statment at the very end of the code by calling the remove method at the end. Try pasting the given code before the remove statment
eboy600 eboy600

2021/6/15

#
Roshan123 wrote...
eboy600 wrote...
i put this code into my game but it came up with this error when they touch
Is thier any remove statement? If yes then note that the remove statement should always be terminated at the end. Try pasting the given code before the remove statment
there is not a remove statement.
Roshan123 Roshan123

2021/6/15

#
Plz try pasting the given code at the very first line of act method. If it doesn't work then plz wait
danpost danpost

2021/6/15

#
eboy600 wrote...
there is not a remove statement.
Please provide full class codex -- MyWorld, Hook and Bottle.
eboy600 eboy600

2021/6/18

#
danpost wrote...
eboy600 wrote...
there is not a remove statement.
Please provide full class codex -- MyWorld, Hook and Bottle.
sorry i forgot password, i have also changed how i want to do my game instead i would like to remove the plastic when it touches the hook and then add one to the score, right now I have a counter on screen but when the plastic dies nothing is added to score
eboy600 eboy600

2021/6/18

#
MY WORLD import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class MyWorld extends World { public static int score = 0; public Bottle Bottle; private counter theCounter; public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(700, 700, 1); theCounter = new counter(); addObject(theCounter, 610, 670); prepare(); } public counter getCounter() { return theCounter; } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { white white = new white(); addObject(white,349,44); Manm manm = new Manm(); addObject(manm,225,132); manm.setLocation(233,135); Rope1 rope1 = new Rope1(); addObject(rope1,321,340); white.setLocation(480,61); white.setLocation(366,12); white.setLocation(134,69); white.setLocation(58,-44); removeObject(white); manm.setLocation(347,40); white = new white(); addObject(white,347,40); manm.setLocation(223,123); rope1.setLocation(321,340); hook hook = new hook(); addObject(hook,315,652); } }
eboy600 eboy600

2021/6/18

#
BOTTLE import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Bottle extends Actor { public void act() { move(2); hook hook = new hook(); //Act method if(isTouching(hook.class)) if ( Greenfoot.getRandomNumber(10)<1) { turn (Greenfoot.getRandomNumber(90)-45); } if (isAtEdge()) { turn(180); } if (getY() < 360) setLocation(getX(), 360); } }
eboy600 eboy600

2021/6/18

#
HOOK import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class hook extends Actor { public Bottle Bottle; MyWorld thisGame; counter score; public void act() { if(Greenfoot.isKeyDown("down")) { setLocation(getX(), getY()+5); } if(Greenfoot.isKeyDown("up")) { setLocation(getX(), getY()-5); } if (getY() < 312) setLocation(getX(), 312); // 315 312 Actor Bottle = getOneIntersectingObject(hook.class); removeTouching(Bottle.class); thisGame.score++; } }
eboy600 eboy600

2021/6/18

#
COUNTER import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class counter extends Actor { private int score = 0; public counter() { setImage(new GreenfootImage("Score : ", 24, Color.BLACK, Color.WHITE)); } public void bumpCount(int amount) { score += amount; setImage(new GreenfootImage("" + score, 20, Color.WHITE, Color.BLACK)); } }
danpost danpost

2021/6/21

#
In Bottle class,, remove:
 
        hook hook = new hook();
 
            //Act method
                if(isTouching(hook.class))
In hook class, change:
Actor Bottle = getOneIntersectingObject(hook.class);
to
Actor bottle = getOneIntersectingObject(Bottle.class);
and follow it up with
if (bottle != null)
{
    // (code)
}
danpost danpost

2021/6/21

#
All in all, in hook class, using the following is easier:
if (isTouching(Bottle.class))
{
    removeTouching(Bottle.class);
    ((MyWorld)getWorld()).getCounter().bumpCounter(1);
}
(removing public static int score = 0; from MyWorld).
eboy600 eboy600

2021/6/22

#
danpost wrote...
All in all, in hook class, using the following is easier:
if (isTouching(Bottle.class))
{
    removeTouching(Bottle.class);
    ((MyWorld)getWorld()).getCounter().bumpCounter(1);
}
(removing public static int score = 0; from MyWorld).
thank you very much, this works perfectly and i appriciate it man
There are more replies on the next page.
1
2