Game Developement and Coding 6 March 2014 – Rubbish Bin Class
|
Rubbish Bin Class. Might not sound all too exciting, but rubbish bins in our game space play an important role for the game play. They can be knocked over by the player and when this is done a power up might spawn by the bin. Otherwise there will only be trash coming out of it. The basics of the class were simple enough to code. I simply made a new class type with its own sprite, collider (for checking collisions) and variables, like speed. The problem lay in displaying the “knocked over”-animation when it was hit by the players’ attack, making it fall away from the players’ position as well as give proper coordinates for where to spawn the trash, alternatively the power up. Starting with the “knocked over”-animation I gave the class a Boolean variable (a variable that can only be “true” or “false” ) called “m_death” that is set to “false” when the object is made, but changes to “true” when the object is struck by the players’ attack. What happens then is that the object is drawn using a separate sprite for the “knocked over”-animation rather than its regular sprite. However you don’t want the rubbish bin falling over time and again. In order to prevent this, and only make the animation run once, the animation loop is made so that when the last frame has been reached it is reset, but not at the beginning. It is reset at the last frame of the animation meaning it will only show that from then on. Rotating the sprite to make the rubbish bin fall away from the player first proved to be a bit of a problem. The rotation of the player is dependent on a variable called “m_angle” which changes depending on the keyboard input. For example, if the input is “up” and “right” the “m_angle” becomes 45. This variable is then used with SFMLs’ rotation function, meaning the player sprite is turned 45 degrees when the “up” and “right” arrows are pressed. When the rubbish bin is knocked over, I send in the same value as “m_angle” to the rotation function used on the rubbish bins’ sprite as well. However, since this happens each update, the knocked over rubbish bins kept on turning as the player avatar was turned in subsequent frames. The solution to this was to give the Rubbish Bin class another Boolean called “m_knockedOver” that is set to “false” when the object is created. Then the rotation of the sprite is set within an if-statement that is only executed while the “m_knockedOver” is “false”. When the rotation is applied for the first time the “m_knockedOver” is also changed to “true” in the same if-statement, meaning it will never be rotated again. The same “m_angle” variable was also used to determine where the rubbish/power up should spawn. If the bin fell to the left, in other words if the bin had the angle value of 270, then the spawn point should be placed to the left of the bin. This was done by sending the angle value into a method that then added numbers to the bins’ location depending on the angle value. For example, if the angle value sent into the method was 270, then the bins’ location had its x-value decreased but its y-value unchanged in order to move it to the left of the bin. The “POW” sprite is a placeholder for the rubbish sprite. |
