TOUR
PRICING
HELP
Stencylpedia
Blocks Guide
Sample Games
Developer Center
FORUMS
CHAT
#MADEINSTENCYL
EDUCATION
SIGN IN
×
Welcome back!
Username or E-mail
Password
(
Forgot?
)
C
OMMUNITY
Home
Issue Tracker
Inbox [0]
New Posts
New Replies
Search
Stencyl Community
→
Stencyl
→
Ask a Question
→
Selecting a random actor on screen
Pages:
1
Selecting a random actor on screen
TheStrangeOne
Posts: 103
November 10, 2016, 04:25:56 am
I have an 8x8 grid like pattern of actors on screen. I would like to select 3 of them at random every 5 seconds to change their animation. Whats the best way to do this? Would it be using a list or something else? Im having trouble selecting them at random.. Thanks in advance, you guys are always helpful!
gplar
Posts: 1115
November 10, 2016, 06:08:15 am
Are there always 64?
If so you could:
Create a number attribute ActorNumber
Create a number attribute ToBeChanged
Do every five seconds:
Loop three times:
Set ActorNumber to 0
Select a random number ToBeChanged between 0 and 63.
Run through the actors using:
For each actor of type.
If ActorNumber=ToBe Changed
Change Animation for Actor of type
Increase ActorNumber by 1
The risk is that you change the same actor in one run, but it´s a 1:64 chance of it.
« Last Edit: November 10, 2016, 06:55:06 am by gplar »
TheStrangeOne
Posts: 103
November 10, 2016, 04:07:10 pm
thats an interesting way to look at it, ill give it a shot. I wonder if I could also set the ToBeChanged attribute into another attribute and say that if ToBeChanged = UsedNuber it should generate a new number to try and avoid interactin witht eh same actor. Thankyou for the advice anyway! Helps a lot!
TheStrangeOne
Posts: 103
November 10, 2016, 05:05:15 pm
Okay so Im having a small issue:
Because the event is on a scene when I say "switch animation to 'x' for '...' I do not have an option that allows me to change the animation of the actor currently being checked in the loop. Only 'last created', 'choose actor' or 'actor attribute'.
Is there a way to fix this that Im just stupidly missing?
ceosol
Posts: 2279
November 10, 2016, 09:15:55 pm
I would do it with lists
Created
Set actorlist to create new list
Set randomlist to create new list
do every 5 seconds
empty actorlist
empty randomlist
for each actor on screen
add (actor on screen) to list
do until number of items in randomlist = 3
set randomnumber to random number between 0 and number of items in actorlist - 1
if not randomlist contains randomnumber
add randomnumber to randomlist
for each actor on screen
if actor on screen = get item # (get item # 0 from randomlist) from actorlist
switch animation to xxx for actor on screen
otherwise if actor on screen = get item # (get item # 1 from randomlist) from actorlist
switch animation to xxx for actor on screen
otherwise if actor on screen = get item # (get item # 2 from randomlist) from actorlist
switch animation to xxx for actor on screen
gplar
Posts: 1115
November 11, 2016, 05:17:52 am
"Because the event is on a scene when I say "switch animation to 'x' for '...' I do not have an option that allows me to change the animation of the actor currently being checked in the loop. Only 'last created', 'choose actor' or 'actor attribute'."
You drag the "actor of type" label from the loop header down to the animation block. :-)
This refers to the current actor of the loop.
Pages:
1