actionscript-3 - 有人能够解决我的 Adob​​e Animate 游戏中的此故障吗?

标签 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);

提前致谢,只是一个初学者,所以我不确定代码中的问题出在哪里,所以即使是一些指示去哪里查看也会很棒。

最佳答案

因为xDirection *= -1; 要求出现故障。如果球无法一次性脱离该位置,它将永远以 x < 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轴坐标和速度也是如此。

关于actionscript-3 - 有人能够解决我的 Adob​​e Animate 游戏中的此故障吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46537190/

相关文章:

javascript - 更改类 IF 下拉列表和 H2 内容与 Animate.css 匹配某些条件

javascript - 动态添加数据时如何应用 ng-class(?)

jquery - 滚动 div 内的动画图像

javascript - 如何使用外部 AS 文件将 AS3 转换为 HTML5

jquery - 使用 Javascript MVC 构建 Web 应用程序,无需服务器端技术。用什么?

php - 如何将一组 BitmapDatas 转换为视频?

flash - 仅使用开源编译器而非 IDE 学习 Flex 的资源

coldfusion - 无法再访问 CFADMIN

apache-flex - 动态调用函数

oop - 经理类的目的是什么?