从preventing对象被陷在墙壁上和两个点之间快速弹跳 [英] Preventing object from getting stuck on walls and rapidly bouncing between two points

查看:133
本文介绍了从preventing对象被陷在墙壁上和两个点之间快速弹跳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有对象随机填充屏幕,并在舞台上蹦跳。问题在于,一些对象的被卡住和来回弹跳迅速两点之间。我怎样才能prevent呢?

I currently have objects populating the screen randomly and bouncing around the stage. The problem is, some of the objects are getting stuck and bouncing back and forth rapidly between two points. How can I prevent this?

这是我怎么也得为enterFrame设置:

var ballT = e.target
ballT.x +=  ballT.vx;
ballT.y +=  ballT.vy;

if (ballT.x + ballT.width / 2 >= sWidth || ballT.x - ballT.width / 2 <= 0) {
    ballT.vx =  -  ballT.vx;
} else if (ballT.y + ballT.height / 2 >= sHeight || ballT.y - ballT.height / 2 <= 0) {
    ballT.vy =  -  ballT.vy;
}

任何意见或任何好的读值得一试?

Any ideas or any good reads worth checking out?

推荐答案

最有可能的,你的对象会有点太出界,这样,当你打开他们身边,他们没有内部的使它在你的边界重新打开它们。您可能需要改变code是在什么时候扭转乾坤一点更加清晰;

Most likely, your objects are going a bit "too much out of bounds", so that when you turn them around, they don't make it inside the bounds before you turn them around again. You may want to change the code to be a bit more clear on when to turn things around;

var ballT = e.target
ballT.x +=  ballT.vx;
ballT.y +=  ballT.vy;

// Outside to the right and heading right - turn around
if ( ballT.x + ballT.width / 2 >= sWidth && ballT.vx > 0 )
    ballT.vx = -ballT.vx;

// Outside to the left and heading left - turn around
if ( ballT.x - ballT.width / 2 <= 0 && ballT.vx < 0 )
    ballT.vx = -ballT.vx;

// Outside at the bottom and heading down - turn around
if ( ballT.y + ballT.height / 2 >= sHeight && ballT.vy > 0 )
    ballT.vy = -ballT.vy;

// Outside at the top and heading up - turn around
if ( ballT.y - ballT.height / 2 <= 0 && ballT.vy < 0 )
    ballT.vy = -ballT.vy;

这篇关于从preventing对象被陷在墙壁上和两个点之间快速弹跳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆