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

2013/5/6

intersecting

Hawx_ Hawx_

2013/5/6

#
if(getOnIntersectingObject(Plasma.class)){
    setLocation(Greenfoot.getRandomNumber(600), 1);
}   
this is a code in an actor, and I'd like to make it so that when a plasma hits it to goes to a random X co-ord. Thanks, Hawx_
JetLennit JetLennit

2013/5/6

#
Try doing
if(getOnIntersectingObject(Plasma.class) != null){
    setLocation(Greenfoot.getRandomNumber(600), 1);
}   
Hawx_ Hawx_

2013/5/6

#
thanks , works like a dream
JetLennit JetLennit

2013/5/6

#
Just for efficency's sake you should make it
if(getOnIntersectingObject(Plasma.class) != null) setLocation(Greenfoot.getRandomNumber(600), 1);
davmac davmac

2013/5/7

#
Just for efficency's sake you should make it
Hmm, I don't agree. You could save a couple of keystrokes, but it'd be harder to read, more error prone, and it certainly wouldn't be any faster to run.
danpost danpost

2013/5/7

#
@davmac, ignoring the fact that both are equally error-producers (since 'getOnIntersectingObject' will not be found), why would the second version be more error prone? or are you suggesting that if another statement is added for the same condition that the brackets may be missed and then produce unwanted results?
davmac davmac

2013/5/7

#
are you suggesting that if another statement is added for the same condition that the brackets may be missed and then produce unwanted results?
Yes; more lines of code might be added with the expectation that these will be conditionally executed, particularly if the user has a background in a language which uses indentation for scoping (such as Python). I've seen this happen before.
You need to login to post a reply.