As the video link that you see, in this post I will show you how to move the Character 2D Sprites on the game area, but before I get started I will discuss about anything that should be done before moving the 2D character sprites in the game area, certainly on this review I still use 2D game engine library: Golden T game Engine. If you read my previous article titled "animated character 2 Dimensions with GTGE" there is a method named "update" through this metod we can specify the buttons that can be used to move the character sprites in the game.
In this experiment we will make this 2D Charater Sprite move to the left and the right by using the button "left arrow and right arrow" on the keyboard, and the this 2D Charater Sprite is still used as in the previous post that the game's character named "bill rizer" with the 6 columns index and 2 rows index as on the picture below:
First, prepare the three abstract methods as a basic foundation in building a game-based Golden T Game
Engine, if you do not understand how to make the three abstract methods please
visit the following article "animated character 2 Dimensions with
GTGE" and download souce code example, well here we already know that to
to the left we use sprite index from 1 to 6, to the right we use sprite index
from 7 to 14. Implementation of the code is as follows, place the following
function into the metod "update"
As seen in the source code, look at lines of code
numbers 2 to 8, the left button expressed by method "keyDown" with
parameters "KeyEvent. VK_LEFT" in the function above, if the user
presses the left button, then the character sprite will move to the left, where
the method "sethorizontalSpeed" that makes the character sprites move
horizontally as shown in line 4 number code, there are parameters in the method
with a value of (-0.3), minus value is used if we want to move the character
sprites to the left, the value of 0.3 magnitudes of motion speed stating the
character sprites, the greater the value of parameters make the slower
movement.
The code at line 17 to
26 has an important function, when users are not pressing the
left button and the right button then the character sprite will not move as
well as an animated. the code at line number 5 stated that if the character
sprites from the left direction, then stop the animation and point sprite index
frames to 8 as in lines of code number 19, and stop the movement of the
horizontal with the given parameter values of 0.0, as seen on line number 20.
the same functionality shown in lines of code numbers 22 to 26, the difference
is functionality of checking on this line says if the character sprites from
the right direction then stop the animation, and stop the movement of
horizontalnya.
As the final phase we create a function that the character sprite is standing upright and not doing animation when a new game is started, therefore use the frame index number 8 and put on a method "initresources ()" on line number 18. If the source code is executed and the result should be like on the video link above.
Characther Sprite Movement Using GTGE
from here we can draw
conclusions : in order to control the Sprite Characther with keyboard keys, things that
must be done is to put the function of checking in the update method and provide
one additional function to do some checking as the user in not pressing the
left and right keys on the keyboard. For the complete source code can be downloaded at the following link
Characther Sprite Movement Using GTGE
0 comments:
Post a Comment