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

2014/5/27

Comparing Y values of objects of the same class

qwakery qwakery

2014/5/27

#
I'm making a boardgame where you have 2 pieces each. Basically what i'm trying to code is: IF the counter that youve clicked on + the number youve rolled = the y value of your other counter THEN display a message in my message box I cant figure out how to say this in greenfoot!
danpost danpost

2014/5/27

#
Why not use IF 'getOneObjectAtOffset(0, /* number you've rolled */, /* class name */.class)' is not null, THEN ... or IF '!getWorld().getObjectsAt(getX(), getY()+/* number you've rolled */, /* class name */).isEmpty()' THEN ...
qwakery qwakery

2014/5/27

#
So would that be in my case if (getOneObjectAtOffset(0,move,playerOneCounter.class)!=null) {etc etc..} ? (move being the number rolled and playerOneCounter being the name of the player 1 counter class)
qwakery qwakery

2014/5/27

#
And what does null mean btw?
danpost danpost

2014/5/27

#
qwakery wrote...
So would that be in my case if (getOneObjectAtOffset(0,move,playerOneCounter.class)!=null) {etc etc..} ? (move being the number rolled and playerOneCounter being the name of the player 1 counter class)
Yes. And 'null' just means that no object was returned (you can either have a counter found at that location or not -- it is the value returned when 'not' is the case; meaning no object of the class given was found to be at the offset given). It can also be held in a object reference variable. For example:
Actor p1counter = getOneObjectAtOffset(0, move, playerOneCounter.class);
if (p1counter != null) { etc etc.. }
'null', in this case, basically means that no object is currently referenced by the variable.
qwakery qwakery

2014/5/27

#
danpost wrote...
qwakery wrote...
So would that be in my case if (getOneObjectAtOffset(0,move,playerOneCounter.class)!=null) {etc etc..} ? (move being the number rolled and playerOneCounter being the name of the player 1 counter class)
Yes. And 'null' just means that no object was returned (you can either have a counter found at that location or not -- it is the value returned when 'not' is the case; meaning no object of the class given was found to be at the offset given). It can also be held in a object reference variable. For example:
Actor p1counter = getOneObjectAtOffset(0, move, playerOneCounter.class);
if (p1counter != null) { etc etc.. }
'null', in this case, basically means that no object is currently referenced by the variable.
Thanks for that explanation - it's cleared things up nicely :-) In the brackets of the getOneObjectAtOffset, is it how far away it has to look or what space it is looking at? ie. getOneObjectAtOffset(0, 4, playerOneCounter.class); Would that look at coordinate (0,4) for another object, or would it look 0 spaces to the right and 4 spaces up from the counter?
danpost danpost

2014/5/27

#
'getOneObjectAtOffset(dx, dy, clss)' looks to see if the image of any clss type objects are currently at the (getX()+dx, getY()+dy) location.
qwakery qwakery

2014/5/28

#
Cheers
You need to login to post a reply.