I know this is kind of simplistic and that I have kept to the simplistic track for a few days now, but yet again I will post a quite simplistic article. It describes how to have a few different dices which will be independently given a random number. This number will be easy accessible so that you may make a game of it. Maybe this could evolve into the game of Yatzy?
Okay, so this is what I got in my example: Two objects, one controller and one dice object (one for them all, that’s right), also I got a sprite which got seven sub-images. I got one where the dice is blank (for showing when the dices has not yet been rolled) and then the number (represented by dots) one to six.
In create event of the dice object I set image_single to zero, as I don’t want it to roll through all of the sub-images and as I don’t want to display any specific number. Except for that code I only got one, and that’s the alarm event where I will decide the random number:
image_single = floor(random(6))+1;
I want the number of the sub-image to be between one dot and six dots, thus I will want to skip the first sub-image, image zero, which contains no dots. That’s why I choose to first random a number between zero and six and then simply add one.
The controller object will call act as a button, and upon click it’ll randomize all of the dices. I suppose I could have used a global variable to tell the dices when to randomize, but I wanted to show you this piece of code:
for(i=0; (i<instance_number(obj_mdice)); i+=1) {
(instance_find(obj_mdice,i)).alarm[0] = 1;
}
It is highly useful in a lot of different situations. It loops through all of instances of a special object, which you can see by the condition. It then uses the index number to find each instance and set their specific local variable. The greatness of this method is kind of complicated to explain, but if you yet not understand, you should look in the manual for the functions instance_find and instance_number.
Hope this helps anyone, I include an example:
http://gmtutorials.com/files/diceex.gm6
.
Users logged in:
Comments
Loading comments...