I am using tier2 to create a space ship game. I have the constructor of my player class that set its position to middle of the screen but it just appear at left top. Also, i wrote a move function for my player class but it just doesnt move.
template.h
#ifndef _H_AGK_TEMPLATE_
#define _H_AGK_TEMPLATE_
// Link to GDK libraries
#include "AGK.h"
#define DEVICE_WIDTH 1024
#define DEVICE_HEIGHT 768
#define DEVICE_POS_X 32
#define DEVICE_POS_Y 32
#define FULLSCREEN false
// used to make a more unique folder for the write path
#define COMPANY_NAME "My Company"
// Global values for the app
class app
{
class Player *player;
public:
// constructor
app() { memset ( this, 0, sizeof(app)); }
// main app functions - mike to experiment with a derived class for this..
void Begin( void );
int Loop( void );
void End( void );
};
extern app App;
#endif
// Allow us to use the LoadImage function name
#ifdef LoadImage
#undef LoadImage
#endif
template.cpp
// Includes
#include "template.h"
#include "Player.h"
// Namespace
using namespace AGK;
app App;
void app::Begin(void)
{
//Basic app information
agk::SetVirtualResolution (1024, 768);
agk::SetClearColor( 100,170,204 ); // light blue
agk::SetSyncRate(60,0);
agk::SetWindowTitle("Space Shooter");
//Load Sprite
unsigned int player_image = agk::LoadSprite("media/player.png");
player = new Player(player_image);
}
int app::Loop (void)
{
agk::Print( agk::ScreenFPS() );
player->move();
agk::Sync();
return 0; // return 1 to close app
}
void app::End (void)
{
}
Player.h
#pragma once
class Player
{
private:
float move_speed = 10.0f;
unsigned int sprite;
float x, y;
public:
Player();
Player(unsigned int image);
~Player();
//methods
void move_up();
void move_down();
void move_left();
void move_right();
void move();
};
Player.cpp
#include "agk.h"
#include "Player.h"
Player::Player()
{
}
Player::Player(unsigned int image)
{
sprite = agk::CreateSprite(image);
x = agk::GetVirtualWidth() / 2.0f;
y = agk::GetVirtualHeight() - (agk::GetSpriteHeight(sprite) / 2.0f);
agk::SetSpritePosition(sprite, x, y);
}
Player::~Player()
{
}
void Player::move_up()
{
y -= move_speed;
}
void Player::move_down()
{
y += move_speed;
}
void Player::move_left()
{
x -= move_speed;
}
void Player::move_right()
{
x += move_speed;
}
void Player::move()
{
if (agk::GetRawKeyState(39)) move_right();
if (agk::GetRawKeyState(37)) move_left();
if (agk::GetRawKeyState(38)) move_up();
if (agk::GetRawKeyState(40)) move_down();
agk::Print(x );
agk::Print(y);
agk::SetSpritePosition(sprite, x, y);
}