I am trying to make a zombie type of scenario where a zombie heads towards another sprite, once it touches that sprite it then turns that sprite into a zombie and then moves on to the next.
Currently I have the zombie following the person1, but it always follows person1 if I create multiple copies of person1 eg person2, person3, it just follows person1.
I am unable to think of way to make the zombie to either
A follow them all or individual based on the distance they are from the zombie,
or
make the zombie follow the person2 once it has touched and infected person1.
Any suggestions help?
Paul
Offline
Hey there Paul, I will try and help you - I am a big fan of AI Algorithms
1) "Follow all people based on distance"
Construct a script like this. Any words in (brackets) are variables you'll need to make and those in [square brackets] are lists.
Set (counter) to 1
Delete all of [distances]
Repeat {number of people}
Add {Distance to JOIN {person(counter)}} to [distances]
change (counter) by 1
Note - this adds the distances to each person to memory, now you'll need an algorithm to determine the closest person to target
Set (target) to 0
Set (counter) to 1
Set (shortest distance) to 600 {just a large number >= 600}
Repeat {length of [distances]}
if {item (counter) of [distances]} < (shortest distance)
set (target) to (counter)
set (shortest distance) to {item (counter) of [distances]}
change counter by 1
Note - now the variable (shortest distance) will be the distance to the closest person and the number of that person is represented by the variable (target). Now we can go toward the target.
point towards JOIN {person(target)}
move {2} steps
Note - Change the speed to decide the pace of the zombie. Add this script to all zombies and execute it in a loop to always target the closest person.
2) Only target people, not infected people.
Create a variable on all people/zombies, (state). At the beginning, set the (state) of all starting zombies to "zombie" and all people to "people". Redo the script above but with one change which wont allow them to target (state) = "people".
Set (counter) to 1
Delete all of [distances]
Repeat {number of people}
Add {Distance to JOIN {person(counter)}} to [distances]
if (state) of JOIN {person(counter)} = zombie
replace last of [distances] with 999
change (counter) by 1
Set (target) to 0
Set (counter) to 1
Set (shortest distance) to 600 {just a large number >= 600}
Repeat {length of [distances]}
if {item (counter) of [distances]} < (shortest distance)
set (target) to (counter)
set (shortest distance) to {item (counter) of [distances]}
change counter by 1
point towards JOIN {person(target)}
move {2} steps
if (shortest distance) = 999
Note - all people are zombies if the above statement is true, so the game is over.
_______________________
I hope this helps, let me know if you have any other questions I can help with
Prestige
Last edited by Prestige (2012-07-02 13:53:30)
Offline
On this particular topic SciTecCf On all the others I can sit back and let you and amcerbu help out!
@Paul2978 Good luck scripting that, wouldn't want to be you! Its a toughie if you're new to scratch, and welcome also if you are new! Indeed, kamiprestige is my channel, though 'mostly' non-scratch related Btw, if I don't reply to a forum post in a while just drop me a comment on a project so i get a notification and know to check it back - wish forums gave you notifications
Prestige
Offline
Just to clarify also, to make the explanation a little better for the numbers:
" Set (shortest distance) to 600 {just a large number >= 600} "
600 is the maximum possible distance 2 sprites on a stage could be apart. The stage is 480x360, so using pythagoras' sqrt(480^2 + 360^2) = 600. In other words the hypotenuse on the stage is 600 pixels long and is therefore the maximum distance 2 sprites on a stage could be apart. This means every distance value from the list MUST be shorter than 600 so the script won't ever break. You could set the value to 208832852 but as long as it is equal or greater ( >= ) than 600 it will work!
" replace last of [distances] with 999 "
No mathematical rubbish here, its the largest 3 digit integer
Also it must be bigger than 600. If it was 580, then ( incredibly infrequently ) a non-infected person could be 600 pixels away and the zombies would instead auto-track a nearby zombie. So again, this number can be anything as long as its greater than the number you chose before ( ie the 600 )
Offline