【问题标题】:Corona sdk prevent x movement of ballCorona sdk 防止球的 x 运动
【发布时间】:2012-02-10 15:47:46
【问题描述】:

在球跳到 Ractangle 后,我无法阻止球开始移动。矩形以恒定的速度向左移动。球跟随屏幕。当球在矩形上跳跃时,它会从矩形的下落中获得速度。请帮忙!

这是我的一些代码:

--make a box
local box1 = display.newRect( 600, 220, 20, 20 )
box1:setFillColor(255,255,255)
physics.addBody( box1, "static", { friction=0, bounce=0.0 } )

-- make a ball (off-screen) and position it
local ball = display.newImage( "ball.png", 20, 20 )
ball.x, ball.y = 100, 200


-- add physics to the ball
physics.addBody( ball, { density = 1.0, friction = 0, bounce = 0, radius = 19 } )

--rotate the ball
local function rotateBall()
ball.rotation = -365
transition.to( ball, { time=1000, rotation=365, onComplete=rotateBall} )

end
rotateBall()

【问题讨论】:

    标签: game-physics coronasdk


    【解决方案1】:

    如果我理解正确

    • 您正试图让球在来回方向上无限旋转
    • 让球弹起来

    下面的代码可以解决这个问题:

    local physics=require("physics")
    physics.start()
    
    --make a box
    local box1 = display.newRect( 50, 420, 150, 150 )
    box1:setFillColor(255,255,255)
    physics.addBody( box1, "static", { friction=0, bounce=0.0 } )
    
    -- make a ball (off-screen) and position it
    local ball = display.newImage( "scnGame_bird.png", 20, 20 )
    ball.x, ball.y = 100, 200
    
    
    -- add physics to the ball
    physics.addBody( ball, { density = 1.0, friction = 0, bounce = 0.8, radius = 19 } )
    
    --rotate the ball
    
    --ball.rotation = -365
    local rotateBallReverse
    local function rotateBall()
        transition.to( ball, { time=1000, rotation=365, onComplete=rotateBallReverse} )
    end
    
    rotateBallReverse = function()
        transition.to( ball, { time=1000, rotation=-365, onComplete=rotateBall} )
    end
    rotateBall()
    

    对于其余部分,“防止 x 移动”是什么意思?

    【讨论】:

    • 对不起,这不是我想知道的。我所做的是: -- 移动元素的每帧事件 local tPrevious = system.getTimer() local function move(event) --prevent velocity/movement of the ball vx, vy = ball:getLinearVelocity() if vx 0 then ball:setLinearVelocity( 0, 0) end end -- 开始一切移动 Runtime:addEventListener( "enterFrame", move );它所做的是检查每一帧的球的速度,如果它大于或小于 0,则它被设置回 0。
    猜你喜欢
    • 2015-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    相关资源
    最近更新 更多