Sign in to follow this  
ironsam

Direct x9 virtual key not working

Recommended Posts

This program is supposed to make a sonic sprite go left, right, up, down, and vertical but when i press the left to does no do anything then when i press right it does not go right but after i press them times ican go left and right then icant move until i press them again and it just repeats over and over. then when i press up it goes up it works but then if I press down it gose up and it just repeats and it just gose vertical at random i use an engine that i include that has a lot of files that it includes.[img]http://public.gamedev.net//public/style_emoticons/default/huh.png[/img]

#include "..\Engine\Advanced2D.h"
//
using namespace Advanced2D;
#define SCREENW 1024
#define SCREENH 768
#define OBJECT_SPRITE 60
#define OBJECT_BACKGROUND 1
#define MAX 1
Sprite *p;
Texture *ball_image;
Texture *sonic_run_left;
Sprite *cursor;
Sprite *sprite;
Sample *music;
float vy;
float vx;

bool game_preload()
{
g_engine->setAppTitle("INPUT DEMO");
g_engine->setFullscreen(false);
g_engine->setScreenWidth(1024);
g_engine->setScreenHeight(739);
g_engine->setColorDepth(32);
return 1;
}
bool game_init(HWND)
{
//load background image
Sprite *background = new Sprite();
if (!background->loadImage("arz1.png")) {
g_engine->message("Error loading craters.tga");
return false;
}
background->setObjectType(OBJECT_BACKGROUND);
background->setCollidable(false);
g_engine->addEntity(background);

p = new Sprite();
p->loadImage("sonic_run_forward.png");
p->setDirection(0);
p->setScale(1.5f);
//load cursor
cursor = new Sprite();
cursor->loadImage("particle16.tga");

//load asteroid image
sonic_run_left = new Texture();
if (!sonic_run_left->Load("sonic_run_left.png")) {
return false;
}

ball_image = new Texture();
if (!ball_image->Load("lightningball.tga")) {
//g_engine->message("Error loading lightningball.tga");
return false;
}

//create sprites

for (int n=0; n < MAX; n++)
{
//create a new sprite
sprite = new Sprite();
sprite->setObjectType(OBJECT_SPRITE);
sprite->setImage(ball_image);
sprite->setSize(40,40);
sprite->setScale( 2.0 );
sprite->setPosition((SCREENW/2) , (SCREENH/2)-10 );


//add sprite to the entity manager
g_engine->addEntity(sprite);
}

//load independent sample
music = new Sample();
music = g_engine->audio->Load("arz.mp3");
if (!music) {
g_engine->message("Error loading wobble.wav");
return false;
}
//play music
g_engine->audio->Play(music);

return true;
}
void game_update()
{
}
void game_keyPress(int key)
{
/*if(GetKeyState(VK_RIGHT))
{

sprite->setVelocity( -6.0, 6.0 );
}*/

//set velocity
/*
switch (key)
{
*/


//case DIK_DOWN:
if(GetKeyState(VK_DOWN))
{
//vx = spr->getVelocity().getX();
//double vy = spr->getVelocity().getY();
//vx = 0;
vy = 2.0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(8);
sprite->setColumns(8);
sprite->setSize(40,40);
sprite->setFrameTimer(40);
Sleep(1);
//break;
}

if(GetKeyState(VK_UP))
{
//case DIK_UP:
//vx = 0;
vy = -2.0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(8);
sprite->setColumns(8);
sprite->setSize(40,40);
sprite->setFrameTimer(40);
Sleep(1);
//break;
}



if(GetKeyState(VK_RIGHT)== 1)
{
//case DIK_LEFT:
sprite->setImage(sonic_run_left);
vx = -2.0;
//vy = 0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(8);
sprite->setColumns(8);
sprite->setSize(40,40);
sprite->setFrameTimer(40);
//break;
}

if(GetKeyState(VK_LEFT)== 1)
{
//case DIK_RIGHT:
sprite->setImage(ball_image);
vx = 2.0;
//vy = 0;
sprite->setVelocity( vx, vy);
sprite->setTotalFrames(8);
sprite->setColumns(8);
sprite->setSize(40,40);
sprite->setFrameTimer(40);

//break;
}

} //key press
void game_keyRelease(int key)
{
/*
switch (key)
{
case DIK_DOWN:
//vx = 0;
vy = 0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(1);
break;

case DIK_UP:
//vx = 0;
vy = 0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(1);
break;


case DIK_LEFT:
vx = 0;
//vy = 0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(1);
break;


case DIK_RIGHT:
vx = 0;
//vy = 0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(1);
break;
} //switch
*/
if(GetKeyState(VK_UP))
{
//case DIK_LEFT:
sprite->setImage(sonic_run_left);
vx = 0.0;
vy = 0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(1);
sprite->setColumns(1);
sprite->setSize(40,40);
sprite->setFrameTimer(0);
//break;
//Sleep(150);
}
if(GetKeyState(VK_DOWN))
{
//case DIK_LEFT:
sprite->setImage(sonic_run_left);
vx = 0.0;
vy = 0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(1);
sprite->setColumns(1);
sprite->setSize(40,40);
sprite->setFrameTimer(0);
//break;
//Sleep(150);
}

if(GetKeyState(VK_RIGHT))
{
//case DIK_LEFT:
sprite->setImage(sonic_run_left);
vx = 0.0;
//vy = 0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(1);
sprite->setColumns(1);
sprite->setSize(40,40);
sprite->setFrameTimer(0);
//break;
//Sleep(150);
}

if(GetKeyState(VK_LEFT))
{
//case DIK_RIGHT:
sprite->setImage(ball_image);
vx = 0.0;
//vy = 0;
sprite->setVelocity( vx, vy );
sprite->setTotalFrames(1);
sprite->setColumns(1);
sprite->setSize(40,40);
sprite->setFrameTimer(0);
//break;
}

if (key == DIK_ESCAPE) g_engine->Close();
}
void game_mouseButton(int button)
{
switch(button) {
case 0: //button 1
break;
}
}
void game_mouseMotion(int x,int y)
{
}
void game_mouseMove(int x,int y)
{
float fx = (float)x;
float fy = (float)y;
}
void game_mouseWheel(int wheel)
{
}

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

void game_end()
{
delete p;
delete ball_image;
delete cursor;
}

void game_entityUpdate(Advanced2D::Entity* entity)
{
switch(entity->getObjectType())
{
case OBJECT_SPRITE:
Sprite* spr = (Sprite*)entity;
double w = (double)spr->getWidth() * spr->getScale();
double h = (double)spr->getHeight() * spr->getScale();
double vx = spr->getVelocity().getX();
double vy = spr->getVelocity().getY();
if (spr->getX() < 0) {
spr->setX(0);
//vx = fabs(vx);
}
else if (spr->getX() > SCREENW-w) {
spr->setX(SCREENW-w);
//vx = fabs(vx) * -1;
}
if (spr->getY() < 0) {
spr->setY(0);
//vy = fabs(vy);
}
//set floor//
else if (spr->getY() > ((SCREENH)/2)-10) {
spr->setY(((SCREENH)/2)-10);
//vy = fabs(vy) * -1;
}
//spr->setVelocity(vx,vy);
break;
}
}
void game_entityCollision(Advanced2D::Entity* entity1,Advanced2D::Entity* entity2){}
void game_entityRender(Advanced2D::Entity* entity) { }

Share this post


Link to post
Share on other sites
I assume you are using [url=http://msdn.microsoft.com/en-us/library/windows/desktop/ms646301%28v=vs.85%29.aspx]this GetKeyState function[/url]? In that case if the key is pressed then the return value of GetKeyState has the high-order bit set to one, so you probably want to simply check that the return value is non-zero in the key-down function. I am curious as to why you are using GetKeyState and not the key parameter though.

Share this post


Link to post
Share on other sites
I researched key states online and it looked like it would be most useful in that situation.[img]http://public.gamedev.net//public/style_emoticons/default/mellow.png[/img] Edited by ironsam

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this