Animation Breakdown: AI "path-finding"
Added 2019-10-22 18:45:20 +0000 UTCI've shown off quite a bit of the small blob moving around a square, but lets talk about how it actually all works.
With the ball I have it set to move on the simple x & y axis using the following code:
if(roseDirection == 0 && roseGo == true){
rose_mc.x -= roseSpeed;
}else if(roseDirection == 1 && roseGo == true){
rose_mc.y -= roseSpeed;
}else if(roseDirection == 2 && roseGo == true){
rose_mc.x += roseSpeed;
}else if(roseDirection == 3 && roseGo == true){
rose_mc.y += roseSpeed;
}
I have this code in an "ENTER_FRAME" event listener which ticks constantly the moment it enters the frame. This means all I need to do to make the companion go is attach "roseDirection" for her direction and "roseGo" which is her boolean that's set to "true" whenever you want her to move.
There's also standard collision detection that checks if your companion hits a wall.
If she hits a wall, the following code is activated:
function roseDirReset():void
{
trace("Direction Reset");
if(roseDirection == 1){
rose_mc.y = rose_mc.y + 2;
}else if(roseDirection == 3){
rose_mc.y = rose_mc.y - 2;
}else if(roseDirection == 0){
rose_mc.x = rose_mc.x + 2;
}else if(roseDirection == 2){
rose_mc.x = rose_mc.x - 2;
}
}
This code basically shunts the sprite back 2 pixels depending on the direction to stop the sprite from clipping through the wall.
In addition, when the sprite stops moving, it has a 2/10 chance to stop entirely and wait for new input.
It also sets a timeout delay of 500 ticks and checks to see if your companion will move again.
setTimeout(function(){
roseGo = true;
if(roseDirection == rosePrevDir)
{
roseDirection = Math.floor(Math.random()*4);
if(roseDirection == rosePrevDir)
{
roseDirection = Math.floor(Math.random()*4);
if(roseDirection == rosePrevDir)
{
roseDirection = Math.floor(Math.random()*4);
}
}
}else{
roseDirection = Math.floor(Math.random()*4);
}
trace("Rose has decided to continue");
},500);
Enough boring talk Sqwark, talk about the... arrays.
Okay okay keep your well adjusted pants on.
Something you can also see is whenever your companion stops a little exclamation box pops up, and when you click it a little book shows up.
This book and the alert box is brought up using those little grey boxes with the following code:
alertSpawn.addChild(A01);
A01.gotoAndPlay(1);
A01.buttonMode = true;
A01.addEventListener(MouseEvent.CLICK, alertOnClick);
alertOn = true;
Well.. they're added basically the same way.
They spawn at 0,0 of the spawn node.
When the book is spawned it triggers a set of random code that pulls information from separate arrays and merges them together which produces that jumbled letters with the occasional word in it.
Here's the code:
bookSpawn.addChild(B01);
RPT.alphabet.push.apply(this, RPT.wordArray);
B01.page01.text = ""+RPT.alphabet.sort(randomize);
B01.page02.text = ""+RPT.alphabet.sort(randomize);
I'm still very new to Arrays (big thanks to Barn-Flakes for helping me with them) but here's what I believe this is doing.
"RPT.alphabet.push.apply (this, RPT.wordArray); " is taking the two arrays, which is the array with all the letters - 5 of each - with the array with the pre-constructed words, and mixing them together
Then, with the "B01.page01.text " code, it's taking the array and putting it directly onto the page.
I also plan to add a feature that removes the "," from the list to make the letters more jumbled.
So yeah - that's how that very simple "AI" works.
I hope this has been an interesting experience to read, and if you want more like this let me know!