Saturday, 6 September 2014

Character Movement

To give the character the illusion of movement a sprite sheet had to be created.  This sheet contains sprites for the character to be stationary (image 1) sprites for when the character has been hit (images 2-4) as well as sprites for running left (images 5-8) and running right (images 9-12). These sprites were all created using the original character as a base. As the character is now more lively it has been decided that his name is widget. this name fits in well with the theme of the game and works with the steam-punk style.



This image was uploaded into touchdevelop and using the code below a separate action was created to declare the different animation to be used in the game.

private action Animations ()
Defining the sprite parameters
 sprite :=  SpriteSheet → create sprite("4")
 sprite → set height(75)
 sprite → set y( board → height -  sprite → height / 1.5)
Here is where all the animations have been defined for use in the program. This has been done utilizing the sprite sheet.
This is where the sprite stands still
 SpriteSheet → add grid animation("Idle", 1, 1, 0, 100000, false)
Sprite runs right
 SpriteSheet → add grid animation("RRun", 9, 3, 0.5, 100, false)
Sprite runs left
 SpriteSheet → add grid animation("LRun", 5, 3, 0.5, 100, false)
sprite is hit
 SpriteSheet → add grid animation("Hit", 2, 3, 0.5, 100, false)
This sets the first animation to play "Idle"
 sprite → hide
 sprite → create animation → play frames("Idle")
 sprite → show
end action

after testing each animation it was found that they run quite well however there were a few stray pixels and unaligned sprites, these where all fixed and now the sprite sheet runs smoothly.

now that the animations have been declared the sprite needs to be able to move. Another action was created for this purpose and called Run. This determines that if the screen is tapped left the character will move left and if the screen was tapped right the character would run right as long as stop = 1. This variable is key and will be used again later.


private action Run ()
 board → on tap(tapped)
where tapped(x : Number , y : Number) is
if  Stop = 1 then
if x <  board → width / 2 then
 sprite → create animation → stop
 sprite → show
 sprite → create animation → play frames("LRun")
 sprite → set speed x( - 100)
 Direction := 2
else
 sprite → create animation → stop
 sprite → show
 sprite → create animation → play frames("RRun")
 sprite → set speed x(100)
 Direction := 1
end if
else do nothing end if
do nothing
end
end action

This code was placed in the gameloop and ran perfectly. The next section of code is to ensure that the character stays within the screen otherwise players can hide off screen and earn points. this code is as below here the game ends if the player leaves the screen.


private action SpriteLimits ()
This limits the sprite the the confines of the screen. If the sprite leaves the screen the game finishes.
if  sprite → x >  board → width or  sprite → x < 0 then
 Awww2 → play
 sprite → hide
 sprite → set x( board → width / 2)
 sprite → set speed x(0)
 gameOver
else do nothing end if

end action

1 comment:

  1. An effective solution to the movement of your character, Widget.

    ReplyDelete