I am making a side-scrolling platformer game in Flash (Actionscript 3.0). I have made lots of progress lately, but I have come across a new problem.
I will give some background first. My game level's terrain (or 'floor') is referenced by a MovieClip variable called 'floor.' My desire is to have the Player and enemy characters walk along the terrain. I have gotten the Player character to move on the terrain just fine; he walks up/down hills and falls whenever there is no ground beneath him.
Here is the code I created to allow the Player to follow the terrain correctly. Much more code is used to control the Player, but only this code deals with the Player character's following of the terrain and gravity.
// If the Player's not on the ground (not touching the 'floor' MovieClip)...
if (!onGround) {
// Disable ducking
downKeyPressed = false;
// Increase the Player's 'y' position by his 'y' velocity
player.y += playerYVel;
}
// Increase the 'playerYVel' variable so that the Player will fall
// progressively faster down the screen. This code technically
// runs "all the time" but in reality it only affects the player
// when he's off the ground.
playerYVel += gravity;
// Give the Player a terminal velocity of 15 px/frame
if (playerYVel > 15) {
playerYVel = 15;
}
// If the Player has not hit the 'floor,' increase his falling
//speed
if (! floor.hitTestPoint(player.x, player.y, true)) {
player.y += playerYVel;
// The Player is not on the ground when he's not touching it
onGround = false;
}
Since getting this code to work for the Player, I have created a 'SkullDemon' class, which is one of the planned enemies for my game. I want the 'SkullDemon' objects to move along the terrain like the Player does. With lots of great help, I have already coded the EventListeners, etc. necessary for the 'SkullDemons' to move.
Unfortunately, I am having trouble getting them to move along the terrain. In fact, they do not touch the terrain at all; they move along the top of the boundary of the 'floor' MovieClip! I had a simple text diagram showing what I mean, but unfortunately Stackoverflow does not format it correctly. I hope my problem is clear from my description.
Strangely enough, my code for the Player's movement and the 'SkullDemon's' movement is almost exactly the same, yet the 'SkullDemons' do not move like the Player does. Here is my code for the SkullDemon movement:
// Move all of the Skull Demons using this method
protected function moveSkullDemons():void {
// Go through the whole 'skullDemonContainer'
for (var skullDi:int = 0; skullDi < skullDemonContainer.numChildren; skullDi++) {
// Set the SkullDemon 'instance' variable to equal the current SkullDemon
skullDIns = SkullDemon(skullDemonContainer.getChildAt(skullDi));
// For now, just move the Skull Demons left at 5 units per second
skullDIns.x -= 5;
// If the Skull Demon has not hit the 'floor,' increase his falling
//speed
if (! floor.hitTestPoint(skullDIns.x, skullDIns.y, true)) {
// Increase the Skull Demon's 'y' position by his 'y' velocity
skullDIns.y += skullDIns.sdYVel;
// The Skull Demon is not on the ground when he's not touching it
skullDIns.sdOnGround = false;
}
// Increase the 'sdYVel' variable so that the Skull Demon will fall
// progressively faster down the screen. This code technically
// runs "all the time" but in reality it only affects the Skull Demon
// when he's off the ground.
if (! skullDIns.sdOnGround) {
skullDIns.sdYVel += skullDIns.sdGravity;
// Give the Skull Demon a terminal velocity of 15 px/frame
if (skullDIns.sdYVel > 15) {
skullDIns.sdYVel = 15;
}
}
// What happens when the Skull Demon lands on the ground after a fall?
// The Skull Demon is only on the ground ('onGround == true') when
// the ground is touching the Skull Demon MovieClip's origin point,
// which is at the Skull Demon's bottom centre
for (var i:int = 0; i < 10; i++) {
// The Skull Demon is only on the ground ('onGround == true') when
// the ground is touching the Skull Demon MovieClip's origin point,
// which is at the Skull Demon's bottom centre
if (floor.hitTestPoint(skullDIns.x, skullDIns.y, true)) {
skullDIns.y = skullDIns.y;
// Set the Skull Demon's y-axis speed to 0
skullDIns.sdYVel = 0;
// The Skull Demon is on the ground again
skullDIns.sdOnGround = true;
}
}
}
} // End of 'moveSkullDemons()' function
It is almost like the 'SkullDemons' are interacting with the 'floor' MovieClip using the hitTestObject() function, and not the hitTestPoint() function which is what I want, and which works for the Player character.
I am confused about this problem and would appreciate any help you could give me. Thanks!
hitTestPoint
for your "SkullDemon"? It seems like the "SkullDemons" are children of another container (skullDemonContainer). This could cause your issue, if the container position doesn't align with the container wherefloor
belongs to (becausehitTestPoint
expects world (stage) coordinates) \$\endgroup\$envContainer.addChild(floorL1);
andenvContainer.addChild(skullDemonContainer);
. The 'floor' variable is then made equal to 'floorL1.' \$\endgroup\$