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

2014/8/25

Countered problems

1
2
sharifahhilwa sharifahhilwa

2014/8/25

#
I am trying to get my actor eat this Gold. In order to score points but theres syntax error. PLs help check my code if there is any mistake under public void act if (isTouching(Gold.class)) { get(Gold.class); }
K_wow K_wow

2014/8/25

#
Can you please post the code for isTouching() and get() as well?
davmac davmac

2014/8/25

#
And always when you get an error, post the text of the error message.
Super_Hippo Super_Hippo

2014/8/25

#
if (isTouching(Gold.class)!=null)
{
    removeTouching(Gold.class);  
}
This should work. 'get(Gold.class);' probably doesn't work, because you either don't have a method called 'get(...)' or you want to delete an object but pass the class as the parameter.
danpost danpost

2014/8/25

#
@K_wow, the 'isTouching' method is part of the Actor class and you can look at the Actor class API documentation to see what it does. @Super_Hippo, 'isTouching' returns a boolean (a true/false value) which is never null; so you should use
if (isTouching(Gold.class))
//  OR
if (isTouching(Gold.class) == true)
Super_Hippo Super_Hippo

2014/8/25

#
danpost wrote...
'isTouching' returns a boolean (a true/false value) which is never null
That makes sense. I should have read the API, never used that method before....
sharifahhilwa sharifahhilwa

2014/8/27

#
l
sharifahhilwa sharifahhilwa

2014/8/27

#
davmac wrote...
And always when you get an error, post the text of the error message.
reply: @davmac the error msg says "cannot find symbol- method get (java.long.Class<Gold>
danpost danpost

2014/8/27

#
There is no 'get(Class clazz)' method in the Actor class API. In fact, there is no 'get(...)' of any kind, with or without arguments, in the Actor class API. If you are trying to get a reference to the Gold object that the actor is touching, then use:
Gold gold = (Gold)getOneIntersectingObject(Gold.class);
Then, you can do whatever with 'gold' (the Gold object). You can remove it from the world, like so:
getWorld().removeObject(gold);
or whatever. Although, if that is all you wanted to do, then you can just use:
if (isTouching(Gold.class)) removeTouching(Gold.class);
If you need to do more than just remove it, then
if (isTouching(Gold.class))
{
    removeTouching(Gold.class);
    // whatever else needs done -- like  'bumpScore();'
}
sharifahhilwa sharifahhilwa

2014/8/27

#
@danpost yes! it work when used 0.1 and 0.2 but after that, terminal window appear. So what is the current problem now.
sharifahhilwa sharifahhilwa

2014/8/27

#
at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) 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:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Crosser.act(Crosser.java:41) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) 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:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Crosser.act(Crosser.java:41) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2014/8/27

#
at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Crosser.act(Crosser.java:41)
You are using 'getOneIntersectingObject' on line 41 of your Crosser class after 'this' is removed from the world.
sharifahhilwa sharifahhilwa

2014/8/27

#
@danpost im so sorry. i am a noob in progamming and its my graded presentation. I really dont understand what you tried to imply. can you help to elaborate on my mistake.
danpost danpost

2014/8/27

#
You probably have the following line:
getWorld().removeObject(this);
either before line 41 in the act method or in a method called before line 41 in the act method. You cannot use 'getWorld().methodName()', 'getX()', 'getY()', or any other method that requires the actor be in the world after the above line is executed (no intersecting, touching, offset, neighbor or range methods). No further help can be given without seeing your 'act' method. Use the 'code' link below the 'Post a reply' box to insert code into your post.
sharifahhilwa sharifahhilwa

2014/8/27

#
@danpost /** * Act - do whatever the Crosser wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { checkKeys(); if (checkWin()) { endGame(true); } else if(checkDeath() && active == true) { active = false; endGame(false); } Gold gold = (Gold)getOneIntersectingObject(Gold.class); if (isTouching(Gold.class)) removeTouching(Gold.class); }
There are more replies on the next page.
1
2