Tip:
Highlight text to annotate it
X
In this tutorial, I will show you how to use arrays in Alice. I have created a scene
that has three mice on it. I’m going to use an array
to make each mouse say the poem “Three blind mice” in order
Then we are going to add some code, to have them all work together.
Notice, I have an array, called mice. I will show you how I did that
You create a local variable
I am going to give it another name, I will delete it when I am done
you will have to check “is array” and I am just going to call this one
delete. It has to be of a type, we will select my type, myMouse
we have three instances. The name on this is going to be delete me
just to remind me to do that, and I am going to add each of my
mice to the array.
each mouse is given an index number.
Though there are 3 mice, in programming you always start counting at 0.
So the mice in my array are at index of 0
which is this mouse, my first mouse
index 1, which is thisMouse2
and index 2 which is this.mouse3
I’m going to hit cancel, because I already created my mouse array here.
You will notice that once you create your array, the variable m appears
in your instance drop down, so I can select it for my
array. For my array, I had each mouse say
the poem “Three blind Mice”, let’s test it.
Three blind mice, see how they run, they all ran after the farmer’s wife
she cut off their tails with a carving knife, have you ever heard such a tail in your life as three blind mice?
It goes to the next one, so I am going to speed it up
and you see that each one says the poem in order
OK, now I want them all to run towards me
and then I want them to run away, but I want them to do it together
in Alice, each in together is a for loop that makes them each do
the action at one time. So you saw how I brought them in
and selected that it is my mouse array.
Now I want them each to move, and I am gong to have
them move forward ten, and then I will
have them turn, and I want them to turn
right, and do .5, because we want them to face the other way.
and then we want them to run away.
So again we want them to move forward, because forward is always the direction that they are facing.
and this time we will make the number 100. Since
it is happening in the same time period, they will move faster
than when they come toward us. Let’s try it again.
and I’m going to speed through the singing.
Oh no! Only one of them moved. Ah! I made a very
common error that works with arrays, I did not have it
set to my variable of m. Notice there are two variables of m.
This variable m is in scope in the top
loop, this variable m is in scope in the bottom loop.
They are different variables. Here I a can quickly switch this
to be the variable m instead of this.mouse2
Let’s try it again.
And there you go, they ran at us and ran away.
That is how to create, and use, arrays in Alice.