|
This article outlines the steps involved in developing a simple Puzzle Slider game using director.
The first step involves importing the image cut into nine parts of equal sizes. After importing of cast members is done, place all the nine cast members onto the stage in jumbled order in the grid on the stage. Place two buttons on stage with the caption “Reset” and “Done”. Now, we are all set to kick- start the coding part..
In startmovie method, declaration of all the global variables and initialization of the variables is done. Global variables for mouse click count (msclk) , sprite numbers of the two sprites to be swapped, x and y co-ordinates of the two sprites to be swapped, x and y co-ordinates of the all the sprites is declared and initialized.
-- Declaring global variables and
initialization of variables
global msclk,pic1x,pic1y,pic2x,pic2y,spnum1,spnum2
global s1H,s1V,s2H,s2V,s3H,s3V,s4H,s4V,s5H,s5V,s6H,s6V,s7H,s7V,s8H,s8V,s9H,s9V
on startmovie me
msclk=0
pic1x=0
pic1y=0
pic2x=0
pic2y=0
spnum1=0
spnum2=0
-- Storing the initial
co-ordinates of sprites
s1H=sprite(1).locH
s1V=sprite(1).locV
s2H=sprite(2).locH
s2V=sprite(2).locV
s3H=sprite(3).locH
s3V=sprite(3).locV
s4H=sprite(4).locH
s4V=sprite(4).locV
s5H=sprite(5).locH
s5V=sprite(5).locV
s6H=sprite(6).locH
s6V=sprite(6).locV
s7H=sprite(7).locH
s7V=sprite(7).locV
s8H=sprite(8).locH
s8V=sprite(8).locV
s9H=sprite(9).locH
s9V=sprite(9).locV
end
The first and second mouse-clicked image sprites are swapped everytime. Whenever any mouse-click operation on the image sprite is performed, the below mentioned mouseup method is executed. The msclk variable keeps the count of the mousle-click for two sprites being swapped and is reset after every swap operation. Whenever user clicks on any image sprite, the x and y co-ordinates and the sprite number of the sprites is stored. After clicking on the second image sprite, the swapping of the two image sprites is done. The x and y co-ordinates of these to sprites are exchanged and msclk variable is reset.
on mouseUp me
if msclk = 0 then
msclk = 1
-- Storing the values of co-ordinates of the
mouse-click on the first sprite for swapping
pic1x = sprite(me.spritenum).locH
pic1y = sprite(me.spritenum).locV
spnum1 = sprite(me.spritenum).spriteNum
else if msclk =1 then
msclk = 2
-- Storing the values of co-ordinates of the
mouse-click on the second sprite for swapping
pic2x = sprite(me.spritenum).locH
pic2y = sprite(me.spritenum).locV
spnum2 = sprite(me.spritenum).spriteNum
-- swapping the co-ordinate postions of the sprites
sprite(spnum2).locH = pic1x
sprite(spnum2).locV = pic1y
sprite(spnum1).locH = pic2x
sprite(spnum1).locV = pic2y
msclk = 0
else
msclk=0
end if
end
When player is done with the puzzle and clicks on the “Done” button, the click me method is executed. The current x and y co-ordinates of all the nine sprites are compared with the actual x and y co-ordinates to check if the player has cracked the puzzle. If they match, the player is intimated by a dialog displaying “Congrats!!!” otherwise a dialog displaying “Better luck next time” is displayed.
on click me
-- Comparing the current co-ordinates
when user clicks on done button with the correct co-ordinates
if (sprite(1).locH=100 and sprite(1).locV=75 and sprite(2).locH=165 and sprite(2).locV=75 and sprite(3).locH=230 and sprite(3).locV=75 and sprite(4).locH=230 and sprite(4).locV=125 and sprite(5).locH=165 and sprite(5).locV=125 and sprite(6).locH=100 and sprite(6).locV=125 and sprite(7).locH=165 and sprite(7).locV=175 and sprite(8).locH=230 and sprite(8).locV=175 and sprite(9).locH=100 and sprite(9).locV=175) then
alert ("Congrats!!!!!!!!")
sound(1).play(member("win"))
else
alert ("Better luck
next time")
end if
end
When player clicks the “Reset” button, all the image sprites take up the jumbled positions again and everything is set for the player to solve the puzzleslider game again. The click me method is executed when player clicks on the “Reset” button and all the image sprites are assigned the x and y co-ordinates stored initially for the jumbled up positions. Also, other global variables are reset.
Feedback:
If you have
any questions or comments concerning this article, please send a message to
poonam308y@yahoo.com
|