RPGConcept — Avatar

Cheng Xi Tsou
3 min readAug 25, 2018

As with most RPG games or games in general, the playable character is the central focus on the screen. In my game, I’ve borrowed the sprite sheet from the game Realm of the Mad God to create my avatar. Since the sprite sheet came with all 18 roles in the game ROTMG, I’ve decided to implement them all.

My Avatar class is divided into two major categories, rendering the part and the logical part. The avatar class stores values such as int[] stats, positionX, positionY, moveSpeed, Array<Item> inventory (I’ll talk about this in a later post), and also handles the rendering of the character along with different animations.

Firstly, I used TextureRegion to split the sprite sheet into many tiles to be used for animation. An example of a loop used to initiate the animation:

int index = 0;
for(int i = (classOrder-1)*3+1; i < ((classOrder-1)*3+3); i++) {
index = 0;
for(int j = 0; j < 3; j++) {
if(i == (classOrder-1)*3+1) {
walkFront[index++] = sheet[i][j];
}
if(i == (classOrder-1)*3+2) {
walkBack[index++] = sheet[i][j];
}
}
}

I had created an enum class to store values for individual classes, and one of them being classOrder, which was the position of that class on the sprite sheet. I needed to reference classOrder in my loop so that the loop would initialize the correct animation for the character role.

The avatar had 2 different sets of animations, one for walking and one for attacking. Getting the attack animation to work was a hurdle since the sprite was actually 8 x 13 where the walking sprite was 8 x 8. The solution was to scale down the attack animation when it rendered so it looked natural. As for the animation loop time, I had scaled it with the character role’s dexterity stat for attacking, and the speed stat for walking.

Overall, creating the avatar wasn’t a difficult task, but managing the different methods and variables in the avatar class was tough. The avatar class had to handle inputs, rendering the correct TextureRegion for the specific time frame, and store the character’s stats. The most important part of making the avatar class is definitely the rendering part. Since the TextureRegion isn’t actually centered correctly, when the avatar moves from left to right, the “tail” of the sprite shifts its position dramatically. In order to prevent this from happening, I had to draw the frame where the character is facing left slightly to the right so the avatar looks centered. Another hard task was to get the avatar to read the input of the mouse. I had to calculate the angle from the avatar’s center to the mouse’s x and y position and have different attack animations load depending on the angle.

Originally published at http://noviceprogramming667661022.wordpress.com on August 25, 2018.

--

--