Hello, I'm new to Adobe Animate. I had trouble creating an HP bar for my character while creating a game in Adobe Animate. When the enemy touches/hits the character, their health will decrease. Also, I encountered a bug with the coding that caused my character to attack the enemy when the spacebar key was pressed. I'd be grateful if someone could teach me Adobe Animate and help me T_T. Btw, here is my code :
var jumpPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var xSpeed:Number = 0;
var xSpeedMulti:Number = 5.5;
var xSpeedDiv:Number = 0.75;
var backPosX:Number = 0;
var backPosY:Number = -36;
var sideCheckHit:Boolean = false;
var leftRightSwitch:Boolean;
var startX:Number = 201.7;
var startY:Number = 291.85;
var ySpeed:Number = 0;
var jumpMulti:Number = 15;
var gravityMulti:Number = 1.5;
var groundCheckHit:Boolean = false;
var playerScale:Number = player.scaleX;
var score:int = 0;
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed);
function gameLoop(event:Event)
{
if (jumpPressed && groundCheckHit)
{
ySpeed = -jumpMulti ;
}
if (leftPressed)
{
xSpeed -= xSpeedMulti;
}
if (rightPressed)
{
xSpeed += xSpeedMulti;
}
groundCheck.x = player.x;
groundCheck.y = player.y + 70;
xSpeed *= xSpeedDiv;
backPosX -= xSpeed;
backPosY -= ySpeed;
background.x = backPosX;
background.y = backPosY;
if (background.hitTestPoint(groundCheck.x, groundCheck.y, true))
{
groundCheckHit = true;
}
else
{
groundCheckHit = false;
}
if(groundCheckHit)
{
ySpeed = 0;
}
else
{
ySpeed += gravityMulti;
}
if (background.hitTestPoint(player.x, player.y, true))
{
sideCheckHit = true;
}
else
{
sideCheckHit = false;
}
if (sideCheckHit)
{
if (leftRightSwitch)
{
backPosX -= 7;
xSpeed = 0;
}
else
{
backPosX += 7;
xSpeed = 0;
}
}
//animation applied for character
if(groundCheckHit)
{
if(xSpeed > 1)
{
player.scaleX = playerScale;
player.gotoAndStop("run");
}
else if(xSpeed < -1)
{
player.gotoAndStop("run");
player.scaleX = -playerScale;
}
else
{
player.gotoAndStop("idle");
}
}
else
{
player.gotoAndStop("jump");
}
if (background.fallDeath.hitTestObject(groundCheck)) {
backPosX = startX;
backPosY = startY;
player.x = startX;
player.y = startY;
ySpeed = 0;
}
//Scoring Board
if(player.hitTestObject(background.point1))
{
score ++;
background.removeChild(background.point1);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point2))
{
score ++;
background.removeChild(background.point2);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point3))
{
score ++;
background.removeChild(background.point3);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point4))
{
score ++;
background.removeChild(background.point4);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point5))
{
score ++;
background.removeChild(background.point5);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point6))
{
score ++;
background.removeChild(background.point6);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point7))
{
score ++;
background.removeChild(background.point7);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point8))
{
score ++;
background.removeChild(background.point8);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point9))
{
score ++;
background.removeChild(background.point9);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point10))
{
score ++;
background.removeChild(background.point10);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point11))
{
score ++;
background.removeChild(background.point11);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point12))
{
score ++;
background.removeChild(background.point12);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point13))
{
score ++;
background.removeChild(background.point13);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point14))
{
score ++;
background.removeChild(background.point14);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point15))
{
score ++;
background.removeChild(background.point15);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point16))
{
score ++;
background.removeChild(background.point16);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point17))
{
score ++;
background.removeChild(background.point17);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point18))
{
score ++;
background.removeChild(background.point18);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point19))
{
score ++;
background.removeChild(background.point19);
scoreText.text = "Score: " + String(score) + "/20";
}
if(player.hitTestObject(background.point20))
{
score ++;
background.removeChild(background.point20);
scoreText.text = "Score: " + String(score) + "/20";
}
}
function fl_SetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
jumpPressed = true;
break;
}
case Keyboard.LEFT:
{
leftPressed = true;
leftRightSwitch = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
leftRightSwitch = false;
break;
}
case Keyboard.A:
{
leftPressed = true;
leftRightSwitch = true;
break;
}
case Keyboard.D:
{
rightPressed = true;
leftRightSwitch = false;
break;
}
}
}
function fl_UnsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.UP:
{
jumpPressed = false;
break;
}
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
case Keyboard.A:
{
leftPressed = false;
break;
}
case Keyboard.D:
{
rightPressed = false;
break;
}
}
}