首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在波浪运动中移动html5 (createjs)中的对象

如何在波浪运动中移动html5 (createjs)中的对象
EN

Stack Overflow用户
提问于 2013-09-12 08:36:29
回答 2查看 1.4K关注 0票数 0

我从这里学习html5和画布交互

http://www.adobe.com/devnet/createjs/articles/getting-started.html

以下是代码的某些部分

代码语言:javascript
复制
    function handleComplete() {
    exportRoot = new lib.PlatypusGame();
    exportRoot.removeChild(exportRoot.platypus);

    stage = new Stage(canvas);
    stage.addChild(exportRoot);

    Touch.enable(stage);

    Ticker.setFPS(20);
    // add the listener to window, so we can do some work before updating the stage:
    Ticker.addListener(window);
}

function tick() {
    if (platypii.length < 1 || Math.random() < 0.01 && platypii.length < 5) {
        var platypus = new lib.Platypus();
        platypus.scaleX = platypus.scaleY = Math.random()*0.3+0.3;
        platypus.x = 800;
        // nominalBounds holds the dimensions of the first frame of the symbol at export time.
        platypus.y = Math.random()*(400-platypus.scaleY*platypus.nominalBounds.height);
        platypus.velX = (1+platypus.scaleX)*-6;
        platypus.velY = 0;
        // we only want to know about clicks on the balloon, not the whole platypus:
        platypus.platypusIdle.balloon.onClick = handleBalloonClick;
        platypus.onPopped = handleBalloonPopped;

        platypii.push(platypus);
        exportRoot.addChild(platypus);
    }

    // go in reverse to make it easier to splice items from the array
    for (var i=platypii.length-1; i>=0; i--) {
        platypus = platypii[i];

        // add gravity to the Y velocity if it's falling:
        if (platypus.falling) { platypus.velY += 3; }
        platypus.x += platypus.velX;
        platypus.y += platypus.velY;

        if (platypus.x < -platypus.nominalBounds.width*platypus.scaleX || platypus.y > 400) {
            platypii.splice(i,1);
            exportRoot.removeChild(platypus);
            // add +100 points if it fell or -500 if it escaped
            updateScore(platypus.y > 400 ? 100 : -500);
        }
    }

    stage.update();
}

我试图通过将platypus.velY = 0;改为platypus.velY = Math.sin(platypus.x) *5来改变它--即鸭嘴兽在波浪中移动,但没有成功,有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-09-12 09:02:03

你需要加一个滴答计数器,波动应该是定期的.

在每一滴答()开始时,你把计数器增加1 tickCounter++;,你的速度是:

代码语言:javascript
复制
platypus.velY = Math.sin(tickCounter * frequency) * amplitude

然而,由于JS计算中的不规则性,-时间实际上可能会移动platypus,因此,如果您希望对象保持在相同的位置上并上下波动,那么执行以下操作更安全:

代码语言:javascript
复制
// at init
platypus.baseY = platypus.y = Math.random()*(400-platypus.scaleY*platypus.nominalBounds.height);

// in the tick()
platypus.y = platypus.baseY + Math.sin(tickCounter * frequency) * amplitude;

加法

如果您想让您的鸭嘴兽在同一时间内不是全部波动,那么您可以在init中为每个鸭嘴兽添加一个滴答-偏移量:platypus.tickOffset = Math.random()*2*Math.PI,然后在勾中使用这个:

代码语言:javascript
复制
platypus.y = platypus.baseY + Math.sin((tickCounter+platypus.tickOffset) * frequency) * amplitude;
票数 0
EN

Stack Overflow用户

发布于 2014-01-21 18:09:13

哈哈好玩!这个怎么样?

derossi/xAy7u/10/

代码语言:javascript
复制
if (platypii.length < 1 || Math.random() < 0.01 && platypii.length < 5) {
    var platypus = new lib.Platypus();
    platypus.scaleX = platypus.scaleY = Math.random() * 0.3 + 0.3;
    platypus.x = 800;
    platypus.angle = 0;
    platypus.inc = Math.random() - 0.2;
    // nominalBounds holds the dimensions of the first frame of the symbol at export time.
    platypus.y = Math.random() * (400 - platypus.scaleY * platypus.nominalBounds.height);
    platypus.velX = (1 + platypus.scaleX) * -6;
    platypus.velY = 0;

    //platypus.y = platypus.velY;
    //platypus.velY = Math.sin(tickCounter * platypus.velX) * 5;
    // we only want to know about clicks on the balloon, not the whole platypus:
    platypus.platypusIdle.balloon.onClick = handleBalloonClick;
    platypus.onPopped = handleBalloonPopped;

    platypii.push(platypus);
    exportRoot.addChild(platypus);
}

..。

代码语言:javascript
复制
for (var i = platypii.length - 1; i >= 0; i--) {
    platypus = platypii[i];
    platypus.velY = Math.sin(platypus.angle) * 10;
    platypus.angle += platypus.inc;
    if (platypus.falling == true) {
        platypus.velY = 0;
        platypus.velY += 30;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18759369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档