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

2016/3/18

how to change value of variable when hits the edge

Shadow7 Shadow7

2016/3/18

#
hey there everyone! so i need to know how to change the value of a variable when say an object hits the edge i have tried a couple things but i cannot get it to work. what i am trying to do is once this object reaches an edge it will multiply the value of the variable by -1 ( so if it was a positive it would change to a a negative and then change the variable to a negative one and move it to the left). i tried something like
int mv=1;
if(isAtEdge())
{ mv=mv*1 }
move(mv); 
im a newbie to this and looking for help so dont just tell me to RTFM
danpost danpost

2016/3/18

#
Shadow7 wrote...
what i am trying to do is once this object reaches an edge it will multiply the value of the variable by -1
Line 3 in the code given is multiplying by positive one, not multiplying by negative one. You need the variable 'mv' to be maintained longer than during the single execution of the method; so, you need to move line 1 outside the method. This changes it from being a local variable to being an instance field which is maintained for as long the actor object exists.
You need to login to post a reply.