Author Topic: Moving player controlled Entity  (Read 135 times)

0 Members and 1 Guest are viewing this topic.

Offline Kitamasu1

  • Regular Member
  • *
  • Join Date: May 2017
  • Posts: 11
  • Karma: 2
  • Game: Morrowind
  • Genre: RPG
  • Platform: PC/Steam
  • Studio: Blizzard
Moving player controlled Entity
« on: May 12, 2017, 01:08:02 PM »
I'm working on my first game project using the Advanced2D engine, and I can't quite figure out how to get a player controlled sprite to move. In the game_keyPress() function I search for the player_ship ObjectType which is unique to the player controlled sprite. Then I set the velocity of the sprite to a different value depending on what button is pressed, -2 for Up or Left, and +2 for Down or Right. In the non-minimal project I can get an enemy sprite to move down across the screen, but the player sprite is completely immobile. Am I just missing something? The engine automatically handles the moving and animation of the sprites.

Code: [Select]
#include "..\Engine\Advanced2D.h"
using namespace Advanced2D;

#define SCREENW 1280
#define SCREENH 800

Sprite *player;

SideScroller *parallax_starfield;

bool game_preload()
{
g_engine->setAppTitle("Hyperspace Wars: Intergalactic Conflict");
g_engine->setFullscreen(false);
g_engine->setScreenWidth(SCREENW);
g_engine->setScreenHeight(SCREENH);
g_engine->setColorDepth(32);

return true;
}

bool game_init(HWND)
{
parallax_starfield = new SideScroller();
parallax_starfield->load("parallax_starfield.png");
parallax_starfield->setViewport(0, 0, 2560, 2560);
parallax_starfield->setColor(0xDDFFFFFF);

//player sprite
player = new Sprite();
player->loadImage("player_ship.png");
player->setCollidable(true);
player->setCollisionMethod(COLLISION_RECT);
player->setSize(64,64);
player->setVisible(true);
player->setVelocity(0, 0);
player->setPosition(SCREENW/2, SCREENH/2);
g_engine->addEntity(player);

return true;
}

void game_update() { }

void game_end()
{
delete parallax_starfield;

delete player;
}

void game_render3d()
{
g_engine->ClearScene(D3DCOLOR_XRGB(20, 0, 20));
g_engine->SetIdentity();
}

void game_render2d()
{
//draw the scroller
parallax_starfield->update(-2);
parallax_starfield->draw();
}

void game_keyRelease(int key)
{
if (key == DIK_ESCAPE)
g_engine->Close();
}

void game_mouseButton(int button, int posX, int posY)
{
}

void game_keyPress(int key)
{
Sprite *p_ship = (Sprite*)g_engine->findEntity(player_ship);

switch(key)
{
case VK_UP: p_ship->setVelocity(p_ship->getVelocity().getX(), -2); break;
case VK_DOWN: p_ship->setVelocity(p_ship->getVelocity().getX(), +2); break;
case VK_LEFT: p_ship->setVelocity(-2, p_ship->getVelocity().getY()); break;
case VK_RIGHT: p_ship->setVelocity(+2, p_ship->getVelocity().getY()); break;
}
}

void game_mouseMotion(int x, int y) { }
void game_mouseMove(int x, int y) { }
void game_mouseWheel(int wheel) { }
void game_entityUpdate(Advanced2D::Entity* entity)
{
Sprite *sprite = (Sprite*)entity;

if (sprite->getObjectType() == player_ship)
{
sprite->setX(sprite->getX() + sprite->getVelocity().getX());
sprite->setY(sprite->getY() + sprite->getVelocity().getY());
}
}
void game_entityRender(Advanced2D::Entity* entity)
{
entity->setVisible(true);
entity->draw();
}
void game_entityCollision(Advanced2D::Entity* entity1, Advanced2D::Entity* entity2) { }



Offline twobitcoder

  • Administrator
  • -[ EPIC MEMBER ]-
  • *****
  • Join Date: Mar 2007
  • Posts: 4,931
  • Karma: 69
    • jharbour.com
  • Game: Adventure
  • Genre: RPG
  • Platform: Atari
  • Studio: Activision
Re: Moving player controlled Entity
« Reply #1 on: May 17, 2017, 03:23:41 PM »
Hello. Sorry for late reply. This is an old book now so I wasn't keeping an eye on the posts. And....the forum doesn't seem to work right any more because I never get notifications. :(

Where did you declare player_ship? I didn't see that set to any value.

   Sprite *p_ship = (Sprite*)g_engine->findEntity(player_ship);


Next, you declared player as a public so it's visible everywhere. You can just use that pointer/reference any time you want to manipulate the player sprite without searching the list for it.

Those are my first suggestions after spending a few mins looking over your code.

I'm travelling for the next few days so it will be at least that long before I am able to reply again but I'll check back as soon as I can.

Offline Kitamasu1

  • Regular Member
  • *
  • Join Date: May 2017
  • Posts: 11
  • Karma: 2
  • Game: Morrowind
  • Genre: RPG
  • Platform: PC/Steam
  • Studio: Blizzard
Re: Moving player controlled Entity
« Reply #2 on: May 18, 2017, 12:09:55 PM »
I'll have to try these suggestions tomorrow as I have to leave for work in a little and won't be doing any programming afterwards.

The "player_ship" object type I coded directly into the Engine. I didn't really think about using define statements and integers, but I saw that's what you did with the Blocks game. So it'll just be a matter of changing things over to that way so that I don't have to keep adding new object types to the Engine for every project.

Offline Kitamasu1

  • Regular Member
  • *
  • Join Date: May 2017
  • Posts: 11
  • Karma: 2
  • Game: Morrowind
  • Genre: RPG
  • Platform: PC/Steam
  • Studio: Blizzard
Re: Moving player controlled Entity
« Reply #3 on: May 19, 2017, 09:54:52 AM »
I removed the search for "player_ship" and just manipulated the pointer, and nothing changed. So I added a Font object that would show the velocity of the ship, and when I press the appropriate key, the values aren't changing.

In a larger project, I have an enemy ship that moves down across the screen, also an Entity, but the player object remains immobile as in this bare minimum code. I don't see how I'm treating the two objects differently in that project, but I'm not entirely sure why the velocity won't change.

Offline twobitcoder

  • Administrator
  • -[ EPIC MEMBER ]-
  • *****
  • Join Date: Mar 2007
  • Posts: 4,931
  • Karma: 69
    • jharbour.com
  • Game: Adventure
  • Genre: RPG
  • Platform: Atari
  • Studio: Activision
Re: Moving player controlled Entity
« Reply #4 on: June 04, 2017, 08:47:44 AM »
You're declaring a global, Sprite *player, but then (it seems to me) you're also pulling the player object out of the entity manager. You can just use player directly anywhere and it will reference that sprite. Not sure if that will help but that's what caught my attention.

Offline Kitamasu1

  • Regular Member
  • *
  • Join Date: May 2017
  • Posts: 11
  • Karma: 2
  • Game: Morrowind
  • Genre: RPG
  • Platform: PC/Steam
  • Studio: Blizzard
Re: Moving player controlled Entity
« Reply #5 on: June 06, 2017, 10:16:32 AM »
Would I need to check for collisions outside of the Entity Collision function, perhaps in the gameUpdate() function?