Here i go again.
Add 4 Private variables to the object vault.
X_to, number, 0
Y_to, number, 0
X_from, number, 0
Y_from, number, 0
go to the events editor.
First we have to store the Position of the Head in variables before it moves to a new place.
This has nothing to do with the X_head and Y_head variables we made earlier.
They are not holding a position. The head will never be positioned at 0,-10.
X_head and Y_head are holding "relative values"
Relative values have not much meaning by there own. They relate to something. They only get a meaning by adding them to a absolute value. Like we added them to the Head's position to move the Head 10 pixels.
The Values we use to remember Heads old position are absolute value's.
We gonna store Heads X and Y in the variables X_to and Y_to. Because thats what the first body will jump to.
So locate the comment "move head"
there is the condition "every 100 milliseconds" and its sub condition always.
Easier then adding a new sub tree to all this...
is again go by the *lazy editing* way.
Duplicate the always sub-event.
Now you have a duplicate of the event and its actions. And on the right branch in the tree.
We need to store the variables before the Head moves.
So, delete the actions behind the first "always" sub event.
And we are ready to code.
hit new action behind the first "always" sub event.
Step1: self object = Vault
Step2: action = set value
step3: parameters: private variable = X_to / value = Head.X
Yes, Head.x not .x The self where "." refers to is Vault in this action not Head
Duplicate that action.
Double click it.
step3: Private variable = Y_to / value = Head.Y
There, now we we always know where the Head was before it moved.
Now lets snap the first body to that position.
Locate the comment "move body"
Double click the "always" event under it.
Automatically you land in step2 of the events wizard. Thats because "always" has no parameters. Another, you probably think coincidental, reason to make a structure with comments and "always"'s before starting to code. I will try always to start a tree of events with a flow condition.
step2: condition : For Each Object (ordered)
step3: parameters : select Body as object (its a pick condition) / Order expression = Body.UID /
Mode = Ascending
Body.UID and not .UID because its a condition, conditions dont know "selfs", only actions do.
what is the property UID ? it stands for Unique ID. Every object, made in the layout editor or/and made by events, will get an Unique ID, a number. The first made object will have a lower UID then the second made object. Not necessarily 1 and 2. But for sure ascending.
For Each Object (ordered) is a pick condition.
It well run trough the instances of the object chosen in the parameters step. One by one. And feed this instance as picked (one by one) to its sub-events and actions.
It will do that in an order and direction you told it to do.
In this case, it will pick the instances of the object Body. One by one, and feed them one by one to the sub-events and actions we gonna make for it.
It acts as a Loop. And it will adjust its end to the count of instances for Body in this layout. In other words, we can add as many instances of the object body on runtime as it pleases us, the "For Each" will just adjust and take into account all instances.
Now well the condition is in place. Now it needs actions.
First we will store the position of the currently picked instance of Body in a variable. Before we move it.
Lets *lazy edit* . Under the events moving the head u see the action
Vault Set 'X_to' to Head 0 .X
Duplicate it by CTRL + dragging it to the actions of the "For each" we are working on
Double click the copy.
step3: parameters: private variable = X_from / value is Body.x (self = Vault)
Duplicate that action.
Double click it
step3: parameters: private variable = Y_from / value = Body.y
There thats that.
Now we stored its position, we can move the currently picked Body instance.
click new action.
step1: self = Vault
step2: action = set x
step3: parameters: it will be an expression.
double click Vault in the expressions guide
double click the expression "get private variable"
Body.Value('Variable name') will be filled in (you can of course type all that too)
change it to Vault.Value('X_to')
Duplicate the action.
click one step back to step2
step2: action = set Y (select, and click next)
step3: ahh that expression is still here, just change X_to in Y_to
Now all we have to do is change point of view from the current picked instance of Body, to the point of view for the next instance the pick-condition will pick.
For the next instance, the X_from will be the X_to. So all we have to do is store the value of X_from into X_to (same for Y)
X_from is where the previous instance jumped from, and the point the next instance will jump to.
Lets *lazy edit* this.
Duplicate the "Vault Set 'X_to" and the Vault Set 'Y_to' actions from the "move head" events to the place of actions that we are working at.
Double click the "Body: set 'X_to" action (2 actions back under this event)
Copy the expression in it ( Vault.Value('X_to') )
click cancel or finish
double click the first duplicated action. the one that sets the X_to
step3: just paste the clipboard content in the value. And change X_to in X_from
A computer is made to copy and paste, and for nothing else !!!
double click the second duplicated action.
step3: just paste the clipboard content in the value. and change X_to into Y_from
note: every time i make an expression, i will trow it in my clipboard. Usually turns out to be handy.
Run all to check. And it looks like a big mess. And theoretical. The math is right.
Most beginners stop here. At this point. (or at the next problem).
And yet, there error is easy.
We forgot the "every 100 milliseconds" condition. In other words .. Head and body run out of sync.
OK lets sync them up. Add a another ""every 100 milliseconds" condition" ?
lets not do that.
Just drag the event tree with the "For each" loop up, under the second always that moves the head, and on the same tree level.
Delete the "move body" comment.
Edit the "move head" comment to "move head and body"
there, run all again.
Perfect!!!! Only a snake can not move backwards, lol. oh thats easy solved.
And we will in next post.
Here is how the events should look, if you ran in a problem, compare with is picture.
Maybe it was my english, or you forgot a detail somewhere. So check up with this.
I do not want to post the .cap. Because i really need you to type and click all this.
Else it is no tutorial. sorry ?