Im trying to make it so that when right is pressed the player will move right and it will loop through his running animation frames.
Im getting this error:
1 2 3 4
Unhandled exception a 0x00215469 in MyGame.exe: 0xC00000005: Access violation
reading location 0xcdcdce45.
[Break][Continue]
Here is the relevant code:
In Input.cpp
1 2 3 4 5 6
// KeysHeld[] is an array of booleans
bool Input::KeyHeld(int key)
{
return KeysHeld[key];
}
When you use key as the index within KeyHeld( ), you never validate[1] the value held by key. Since you don't validate it, you could be accessing memory that isn't yours.
References: [1] In this context, check if the value is between 0 and X.
This just gives you a pointer. It does not give it an object.
Right now, since the pointer doesn't point to anything, it's a "bad" pointer. When you try to dereference it (ie, with input->KeyHeld), you're trying to access a non-existant object.
You have to either:
1) make it point to an object
1 2
// somewhere in player.cpp when your Player is initialized
input = &some_existing_Input_object;
Say you create a class called Apple. This tells you what an apple is, but isn't an actual apple itself. To actually create an apple, you need objects:
1 2
Apple a;
Apple b;
now we have 2 apples, a and b. a and b are "objects".
Changes made to one object do not impact other objects. Each object exists independently.
So if I do this:
a.TakeABite();
my a Apple will have a bite taken out of it. However the b Apple will remain unchanged (no bite).
I suspect you have 2 or more Input objects. One is in your Player class ("a") and one is somewhere else -- maybe in whatever class handles events ("b"). I suspect you are making changes to "b" and then checking for those changes in "a", which won't work because "a" is a separate object from "b".
I need the game to check each frame if a button is being held down. I cant use the regulare SDL_PollEvent because that is just one event for down, and one event on release.
Could you please guide me in the right direction with some psuedocode?
iirc you don't need to use SDL events for this. SDL has functions for you to get the realtime status of keyboard keys. You can use those instead of having this Input class.
I don't recall the name of the functions, but I'm sure they're in SDL docs. Skim the docs to find them and try using them instead.
okay my code looks like the code below now. The player moves left and right fine, but is not looping through the animation frames.
EDIT: weird stuff is happening. If a directional button and another button are pressed he goes real fast and breaks the scrolling mechanism. And sometimes jumping works, and sometimes is doesnt.