有人能够在我的Adobe Animate游戏中解决这个故障吗?

时间:2017-10-03 04:23:51

标签: actionscript-3 adobe animate.css

Video of my glitch

从视频中可以看出,当球撞到屏幕左侧的某个位置时,球会出现故障,但是,这不会发生在屏幕的右侧。这是我的代码。

var xDirection:Number = 10;
var yDirection:Number = -10;

var targetX:Number = player_mc.x;
var easing:Number = 7;

var playerScore:Number;

function resetBallPosition():void
{
    xDirection = 10;
    yDirection = -10;
    ball_mc.x = 2
    ball_mc.y = 11
}

function checkHitLocation(player:MovieClip):void
{
    var hitPercent:Number;
    var ballPosition:Number = ball_mc.x - player_mc.x;
    hitPercent = (ballPosition / player_mc.width);
    xDirection = hitPercent * 30;
    yDirection *= 1.025;
}

function initializeGame(event:MouseEvent):void
{

    playerScore = 0;
    showScore();
    showStart();
    player_mc.addEventListener(Event.ENTER_FRAME, movePlayer);
    ball_mc.addEventListener(Event.ENTER_FRAME, moveBall);
    bg_mc.removeEventListener(MouseEvent.CLICK, initializeGame);

}



function endGame():void

{

    player_mc.removeEventListener(Event.ENTER_FRAME, movePlayer);
    ball_mc.removeEventListener(Event.ENTER_FRAME, moveBall);
    bg_mc.addEventListener(MouseEvent.CLICK, initializeGame);
    text_mc.text = 'CLICK TO PLAY AGAIN';
    text_mc.visible = true
    start_mc.visible = false;

}

function moveBall(event:Event):void
{
    if(ball_mc.x <= 0)
    {
        xDirection *= -1;
        text_mc.visible = false;
        start_mc.visible = false;
    }
    else if(ball_mc.x >= stage.stageWidth - ball_mc.width)
    {
        xDirection *= -1;
        text_mc.visible = false;
        start_mc.visible = false;
    }
    if(ball_mc.hitTestObject(player_mc))
    {
        yDirection *= -1;
        ball_mc.y = player_mc.y - ball_mc.height - player_mc.height/2;
        checkHitLocation(player_mc);
        playerScore ++;
        showScore();
        text_mc.visible = false;
        start_mc.visible = false;
    }
    if(ball_mc.y <= 0)
    {
        yDirection *= -1;
        //resetBallPosition();
        text_mc.visible = false
        start_mc.visible = false;
    }
    else if(ball_mc.y >= stage.stageHeight - ball_mc.height)
    {
        endGame();
        resetBallPosition();
        showScore();

    }
    ball_mc.x += xDirection;
    ball_mc.y += yDirection;

}

function movePlayer(event:Event):void
{
    if(this.mouseX <= player_mc.width/2)
    {
        targetX = 0;
        start_mc.visible = false;
    }
    else if(this.mouseX >= stage.stageWidth - player_mc.width/2)
    {
        targetX = stage.stageWidth - player_mc.width;
        start_mc.visible = false;
    }
    else
    {
    targetX = this.mouseX - player_mc.width/2;
    start_mc.visible = false;
    }
    player_mc.x += (targetX - player_mc.x) / easing;
}

function showScore():void
{
    score_mc.text = "Score: " + playerScore;
    start_mc.visible = false;
}

function showStart():void
{
    start_mc.text = 'CLICK SCREEN TO START';
    start_mc.visible = true;

}

showStart();


bg_mc.addEventListener(MouseEvent.CLICK, initializeGame);

在此先感谢,只有初学者,所以我不确定我的代码中的问题在哪里,所以即使是在哪里看也会很棒。

1 个答案:

答案 0 :(得分:3)

因为 xDirection * = -1; 要求出现故障。如果球不能一次性逃脱位置,它将永远用 x&lt; 0

你不想否定轴球速度,你想确保它在左侧变为正值而在右侧变为负值。

var aLeft:Number = 0;
var aRight:Number = stage.stageWidth - ball_mc.width;

if (ball_mc.x < aLeft)
{
    // Make it POSITIVE.
    xDirection = Math.abs(xDirection);

    // Fix the position as if the ball ricocheted.
    ball_mc.x = aLeft + (aLeft - ball_mc.x);
}
else if (ball_mc.x > aRight)
{
    // Make it NEGATIVE.
    xDirection = -Math.abs(xDirection);

    // Fix the position as if the ball ricocheted.
    ball_mc.x = aRight + (aRight - ball_mc.x);
}

Y轴坐标和速度也一样。