I'm having a problem with a FPS camera I put in my game. When I press space to jump, it doesnt stop jumping (as if the space key were stuck [which it isn't])
I made a custom keyMap for the camera, heres the code:
P.S. I've been experimenting with the Irrlicht Engine, so I'm not very advanced at it and I wouldn't be surprised if it is something really simple that I forgot lol. (Hopefully it is simple )
Ooops. I'm really sorry, I fixed it. I just realized that I had some code in the OnEvent() to make it jump, and I forgot to remove that before I made my keyMap to do it. But it works now. Thanks.
I did what you said Dances, but then it does what I described in my first post, when the camera jumps continuously like the jump button is stuck. Here is some code
:
And I set hasJumped to false in my constructor of my class. I might not have understood your post correctly because i'm still pretty new to Irrlicht and programming in general. And I might have done something wrong with my class because I just started experimenting with them. Any help would be great, thanks!
When I read "so he only jumps once" I thought you meant only once, and then you have to hit the key again. Not so that he doesn't fly up in the air.
You need to check that hes touching the ground. You don't actually need hasJumped at all for this.
If you want him to not bunny hop when you hold the key you also need to make sure that your event receiver is setting hasJumped to false when the key is let up, not setting it false when the character jumps.
Yes, you should really tell us which errors correspond to each line seeing as that information is provided by the bug, but we cant do that as we dont know the line numbers of the code, you do
I notice your last if statement is a bit strange as it ends in a ; instead of the opening bracket on the line below.
//! Changes the game state, calls the existing states Clear
//! function before the next states Init function
void CGameManager::ChangeState(CGameState * pState)
{
if (m_pGameState)
m_pGameState->Clear(this);
if ( pState != m_pGameState )
{
m_pGameState = pState;
m_pGameState->Init(this);
}
}
//! Holds a pointer to the current states, (level) Update function
//! The Update will be the game loop for the current state
void CGameManager::Update()
{
m_pGameState->Update(this);
}
//! Creates the Irrlicht device and get pointers to the main subsytems
//! for later use, the Game manager is the central interface point to
//! the rendering engine
void CGameManager::CreateDevice()
{
config= new CConfigManager();
// now use CConfigManager to create device, values stored in config.xml
m_pDevice=createDevice(config->getDriverType(), config->getScreenResolution(), config->getBitsperPixel(),
config->getFullscreen(), config->getStencilbuffer(), config->getVsync(), this);
//! Returns an array of keys, used to detect keyboard presses.
//! Each key can be toggled on or off
bool CGameManager::getKey(char key)
{
return m_bKeys[key];
}
//! Clear keys, set all keys to off
void CGameManager::ClearKeys()
{
for(int x=0; x<irr::KEY_KEY_CODES_COUNT; x++) m_bKeys[x] = false;
}
//! Returns a pointer to the Player
CGamePlayer* CGameManager::getPlayer()
{
return m_pPlayer;
}
//! Get Main Meta selector, where all collison triangle selcetors are added
irr::scene::IMetaTriangleSelector* CGameManager::getMetaSelector()
{
return m_pMetaSelector;
}
//! Main event handler derived from IEventHandler, this
//! will be passed down to the current states keyboard handler.
//! The state controls its own keyboard events
bool CGameManager::OnEvent(SEvent event)
{
if (!m_pDriver)
return false;
if (event.EventType == EET_KEY_INPUT_EVENT)
{
if(event.KeyInput.PressedDown){
// if we the console is visible write
if (event.EventType == EET_MOUSE_INPUT_EVENT)
{
// Pass input down to the specific game state mouse handler
m_cMouse = event.MouseInput.Event;
m_pGameState->MouseEvent(this);
}
return false;
}
//! Game manager genral Initialisation function. Also initialise or
//! load each of the plugable managers
void CGameManager::Init()
{
// set initial Font
m_pFont = this->getGUIEnvironment()->getFont("media/font_lucida.bmp");
// load in resourses from archive
this->getDevice()->getFileSystem()->addZipFileArchive("media/map-20kdm2.pk3");
this->getDevice()->getFileSystem()->addZipFileArchive("media/media.zip");
this->getDriver();
// initialise Player
m_pPlayer = new CGamePlayer(0);
m_pPlayer->Init(this);
// initialise Enemy manager
m_pEnemyManager = new CGameEnemyManager();
m_pEnemyManager->Init();
// initialise Item manager
m_pItemManager = new CGameItemManager();
m_pItemManager->Init();
// initialise Path manager
m_pPathManager = new CGamePathManager();
m_pPathManager->Init();
// initialise Weapon manager
m_pWeaponManager = new CGameWeaponManager();
m_pWeaponManager->Init();
// Load FX (Special Effects) Manager
m_pFXManager = new CGameFXManager();
m_pFXManager->Load(this);
// Load Audiere Sound Manager Base on Config.xml sound tag
switch (getConfigManager()->getSoundDevice()){
case AUDIERE :
m_pSoundManager = new CAudiereSoundManager();
break;
case NULLDEVICE:
m_pSoundManager =new CGameSoundManager();
break;
//If we like o use FMod Sound System
#if USEFMOD
case FMODSYSTEM:
m_pSoundManager = new CFmodSoundManager();
break;
//if Not we use Audiere as Default
#else
default:
m_pSoundManager = new CAudiereSoundManager();
break;
#endif
}
m_pSoundManager->Load(this);
}
//! Get Console Manager
CConsole *CGameManager::getConsole(){
return console;
}
//! Gets a Global soft coded Int variable for a specified name, used to pass
//! data accross games states
int CGameManager::getGlobalInt(std::wstring name)
{
std::map<std::wstring,int>::iterator it = m_mIntCache.begin(), stop = m_mIntCache.end();
for( ; it != stop; it ++ )
if (it->first == name)
return it->second;
return -1;
}
//! Sets a Global Int value to the value given. Used to pass data across states.
void CGameManager::setGlobalInt(std::wstring name, int value)
{
m_mIntCache.erase(name);
m_mIntCache.insert(std::pair<std::wstring,int>(name, value));
}
//! Gets a Global soft coded String variable used to pass data between states.
std::wstring CGameManager::getGlobalString(std::wstring name)
{
std::map<std::wstring,std::wstring>::iterator it = m_mStringCache.begin(), stop = m_mStringCache.end();
for( ; it != stop; it ++ )
if (it->first == name)
return it->second;
return (L"");
}
//! Sets a Global soft coded String variable used to pass data between states.
void CGameManager::setGlobalString(std::wstring name, std::wstring value)
{
m_mStringCache.erase(name);
m_mStringCache.insert(std::pair<std::wstring,std::wstring>(name, value));
}
//! Sets global font
void CGameManager::setFont(irr::gui::IGUIFont* font)
{
m_pFont = font;
}
//! Gets global font
irr::gui::IGUIFont* CGameManager::getFont()
{
return m_pFont;
}
//! Keyboard event for state, main keyboard events
//! passed down by Game manager
void CGamePlayState::KeyboardEvent(CGameManager * pManager)
{
// override in child class
}
//! Mouse event handler passed down by CGameManager
void CGamePlayState::MouseEvent(CGameManager* pManager)
{
switch(pManager->getMouse())
{
case EMIE_LMOUSE_PRESSED_DOWN: // left mouse pressed, FIRE!!!
if (!pManager->getWeaponManager()->isWeaponLocked() && pManager->getPlayer()->getAmmo() > 0)
pManager->getWeaponManager()->getActiveWeapon()->ChangeState(WeaponStateShoot::Instance());
break;
case EMIE_RMOUSE_PRESSED_DOWN: // right mouse pressed, RELOAD!!!
if (pManager->getPlayer()->getAmmo() <= 0)
pManager->getPlayer()->setAmmo(100); // 100% full
break;
case EMIE_MOUSE_WHEEL: // change weapon
if (!pManager->getWeaponManager()->isWeaponLocked())
pManager->getWeaponManager()->getActiveWeapon()->ChangeState(WeaponStateChange::Instance());
break;
}
}
//! Initialisation, loads data required for state. Generic game play
//! initialisation performed here, level specific initialisation should
//! be performed by the sub-classed level
void CGamePlayState::Init(CGameManager* pManager)
{
// text position and colour
m_pPlayStateText = pManager->getGUIEnvironment()->addStaticText(L"ESC Quit\n Left Mouse Button Fire\n Right Mouse Button Reload",
rect<int>(10,30,300,160), false);
m_displayText = pManager->getGUIEnvironment()->addStaticText(L"",
rect<int>(10,10,200,30), false);
m_iconText = pManager->getGUIEnvironment()->addStaticText(L"",
rect<int>(355,735,370,745), false);
m_displayText->setOverrideColor(video::SColor(255,255,255,255));
m_iconText->setOverrideColor(video::SColor(255,255,255,255));
m_pPlayStateText->setOverrideColor(video::SColor(255,255,0,0));
// load ammo bar
m_pAmmoBar = pManager->getDriver()->getTexture("media/hud.jpg");
pManager->getDriver()->makeColorKeyTexture(m_pAmmoBar,core::position2d<s32>(0,0));
// load health bar
m_pHealthBar = pManager->getDriver()->getTexture(".media/healthbar.jpg");
pManager->getDriver()->makeColorKeyTexture(m_pHealthBar,core::position2d<s32>(0,0));
//! Update, moves and renders screen. Generic game play updates performed here,
//! level specific updates should be performed by the sub-classed level.
void CGamePlayState::Update(CGameManager * pManager)
{
// overide in child class
pManager->getDriver()->beginScene(true, true, video::SColor(0,100,100,100));
pManager->getSceneManager()->drawAll();
pManager->getGUIEnvironment()->drawAll();
pManager->getEnemyManager()->Update();
pManager->getWeaponManager()->Update();
pManager->getItemManager()->Update(pManager);
displayCrossHair(pManager);
displayAmmoBar(pManager, pManager->getPlayer()->getAmmo());
displayHealthBar(pManager, pManager->getPlayer()->getHealth());
displayIcons(pManager);
pManager->getDriver()->endScene();
if (pManager->getPlayer()->getHealth() <= 0)
if (pManager->getPlayer()->Death(pManager))
ChangeState(pManager, CGameCreditsState::Instance());
}
//! Clear, generic tidy up here, level specific house keeping should be
//! performed by the level specific subclass. Resources that have been
//! initialised should be destroyed also.
void CGamePlayState::Clear(CGameManager* pManager)
{
if (m_pPlayStateText) m_pPlayStateText->remove();
if (m_displayText) m_displayText->remove(); // crashes as leaves state, Visual Studio only, investigate!
if (m_iconText) m_iconText->remove(); // possible swprintf overflow protection. see DsiplayFPS()
pManager->getSceneManager()->clear();
pManager->getEnemyManager()->clear();
pManager->getItemManager()->clear();
pManager->getWeaponManager()->clear();
pManager->getFXManager()->Clear();
pManager->getPathManager()->Clear();
pManager->ClearKeys();
}
//! Displays Renderer, FPS and poly count on the screen
void CGamePlayState::displayFPS(CGameManager* pManager)
{
}
//! Loads the specified Game level (Quake3 .bsp, 3DS Max .3ds or DirectX .x file)
//! and performs general map initialisation. Map position only
void CGamePlayState::loadMap(CGameManager* pManager, const c8* map, core::vector3df mapPosition)
{
loadMap(pManager, map, mapPosition, vector3df(0,0,0));
}
//! Loads the specified Game level (Quake3 .bsp, 3DS Max .3ds or DirectX .x file)
//! and performs general map initialisation. Map and camera position
void CGamePlayState::loadMap(CGameManager* pManager, const c8* map, core::vector3df mapPosition, core::vector3df camPosition)
{
// Load in game Level from resources
m_Selector = 0;
scene::IAnimatedMesh* mesh = pManager->getSceneManager()->getMesh(map);scene::ISceneNode* node = 0;
if (mesh)
node = pManager->getSceneManager()->addOctTreeSceneNode(mesh->getMesh(0));
if (node)
{
node->setPosition(mapPosition);
m_Selector = pManager->getSceneManager()->createOctTreeTriangleSelector(
mesh->getMesh(0), node, 128);
node->setTriangleSelector(m_Selector);
m_Selector->drop();
}
pManager->setMetaSelector();
pManager->getMetaSelector()->addTriangleSelector(m_Selector);
// fog for effect
node->setMaterialFlag(EMF_FOG_ENABLE,true);
pManager->getDriver()->setFog(SColor(0,0,30,0),true, 0,1300);
pManager->getDriver()->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
// set FPS camera and Node Animator for collison detection
scene::ICameraSceneNode* camera = pManager->getSceneManager()->addCameraSceneNodeFPS(0,100.0f,300.0f,-1, loadKeyBindings(), 9);
camera->setPosition(camPosition);
scene::ISceneNodeAnimator* m_pAnim = pManager->getSceneManager()->createCollisionResponseAnimator(
pManager->getMetaSelector(), camera, irr::core::vector3df(30,50,30), irr::core::vector3df(0,-3, 0), irr::core::vector3df(0,35,0));
camera->addAnimator(m_pAnim);
m_pAnim->drop();
// Skybox
loadSkyBox(pManager);
// fade in and disable cursor
pManager->getDevice()->getCursorControl()->setVisible(false);
m_inOutFader = pManager->getDevice()->getGUIEnvironment()->addInOutFader();
m_backColour.set(255,0,0,0); //255 90 90 156
m_inOutFader->setColor(m_backColour);
m_inOutFader->fadeIn(1000);
}
What lines do those errors correspond to? That's an easy thing for you to tell us and then you can just post that line, along with the error and we can probably see what the problem is.
Also, what's stw? I couldn't see anywhere that it was defined.