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

2012/4/17

Take object' coordinates then apply it to another object.

tkt tkt

2012/4/17

#
Hi, I want to create football game and i just want to think how to do it in a simple way.Thats why i just want to make the ball move with the player.For example let say i created an object from player class....player player1=new player()...Then i want to get its coordinates like player1.getX(); player1.getY(); and in the ball class i want to set location when i see the footballer.Thats why i used can see from Animal class of greenfoot.However even if i do like this; if(cansee(player.class)) {int x=player1.getX(); int y=player1.getY(); setLocation(x,y); } Its give error message after player see ball..(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.)So i see i dont understand something because im new at Java but can you help me about how can i fix this problem? PLAYER CLASS import greenfoot.*; public class player extends Actor {double WALKING_SPEED=5.0; public void act() { move(); } public void move() { if (Greenfoot.isKeyDown("left")) {setLocation(getX()-5,getY()); } if (Greenfoot.isKeyDown("right")) { setLocation(getX()+5,getY()); } if (Greenfoot.isKeyDown("up")) { setLocation(getX(),getY()-5); } if (Greenfoot.isKeyDown("down")) { setLocation(getX(),getY()+5); } }} BALL CLASS import greenfoot.*; public class ball extends Actor { public void act() { go(); } public void go(){ if(canSee(player.class)){ player s=new player(); int a=s.getX(); setLocation(getX()+5,getY()); } } public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } }
danpost danpost

2012/4/17

#
You must do more than create an object to get its location. Creating an object does not automatically put that object in the world. You need to find a method to add that object into the world before you can get its location in the world. Let me ask you this (maybe you might get some ideas on how to program this): when the ball and the player 'see' each other, which should control which? I mean, in football, does the football control the player or does the player control the football? Think about what this might imply.
davmac davmac

2012/4/17

#
Please, use the 'code' style for your code. When you're editing a post, click the 'code' link, and paste your code into the dialog box. And: make sure to indent your code properly first.
danpost danpost

2012/4/17

#
Another thing is you do not want to create a new object when another sees that type object. You want to get a reference to that same object that the other object does see. Look at the canSee() method to get an idea of how to accomplish this. You will also need to properly cast the type for that object.
tkt tkt

2012/4/17

#
danpost wrote...
You must do more than create an object to get its location. Creating an object does not automatically put that object in the world. You need to find a method to add that object into the world before you can get its location in the world. Let me ask you this (maybe you might get some ideas on how to program this): when the ball and the player 'see' each other, which should control which? I mean, in football, does the football control the player or does the player control the football? Think about what this might imply.
Thanks for the answers.i know player should see the ball and ball should take the coordinates of player everytime but if i put Cansee to player class than i should think more complicated and i was lazy to do it :)but yes now i understand what error message mean ..Thanks for your replies everyone.
You need to login to post a reply.