如何动画化(默认移动)和非动画化(点击后使其停止)碰撞物体

嘿,尝试这个新的代码来动画对象(这里是气泡),并使用精灵使其保持静止。代码如下:

local ui = require("ui")

local gameUI = require("gameUI")
local easingx  = require("easingx")
require "sprite"

display.setStatusBar( display.HiddenStatusBar )

local physics = require("physics")
physics.start()
physics.setScale( 60 )

local backgroundPortrait = display.newImage( "sky.png", 0, 0 )
local backgroundLandscape = display.newImage( "sky.png", 80, 80 )
backgroundLandscape.isVisible = false
local disp = backgroundLandscape

local function selectBubble( event )
    local tapped = event.target  --event.target是Corona指向被点击的气泡的方法
    if ( tapped.bubbleSelected == false ) then
        local vx,vy = tapped:getLinearVelocity()
        tapped.xVel = vx  --将当前速度存储到气泡的“xVel”变量中
        tapped.yVel = vy  --同样对于yVel
        tapped:setLinearVelocity( 0,0 ) --将气泡的速度设置为0!
        tapped.currentFrame = (3)
        tapped.bubbleSelected = true
    elseif ( tapped.bubbleSelected == true ) then
        tapped:setLinearVelocity( tapped.xVel, tapped.yVel )  --读取以前的速度并设置
        tapped.bubbleSelected = false
    end
end

--气泡1
local bubble1 = sprite.newSprite( spriteSet1 )
bubble1.x = 100
bubble1.y = 100
physics.addBody(bubble1, {bounce=0.04,  filter = bubbleCollisionFilter})
bubble1:setLinearVelocity( 2, 4 )
bubble1:addEventListener( "tap", selectBubble )
bubble1.bubbleSelected = false
bubble1:prepare("bubble")
bubble1:play()

--气泡2
local bubble2 = sprite.newSprite( spriteSet1 )
bubble2.x = 210
bubble2.y = 20
physics.addBody(bubble2, {bounce=0.05,  filter = bubbleCollisionFilter})
bubble2:setLinearVelocity( 2, 4 )
bubble2:prepare("bubble")
bubble2:play()

--气泡3
local bubble3 = sprite.newSprite( spriteSet1 )
bubble3.x = 100
bubble3.y = 17
physics.addBody(bubble3, {bounce=0.02,  filter = bubbleCollisionFilter})
bubble1:setLinearVelocity( 1, 2 )
bubble3:prepare("bubble")
bubble3:play()

--气泡4
local bubble4 = sprite.newSprite( spriteSet1 )
bubble4.x = 310
bubble4.y = 20
physics.addBody(bubble4, {bounce=0.4,  filter = bubbleCollisionFilter})
bubble4:setLinearVelocity( 2, 4 )
bubble4:prepare("bubble")
bubble4:play()

问题是首先该代码似乎无法正常工作。其次,在点击后气泡会改变颜色(每个气泡的颜色相同)。然而,每个气泡上都有一个唯一的字母。如何使其工作?请帮忙。

原文链接 https://stackoverflow.com/questions/6059373

点赞
stackoverflow用户686008
stackoverflow用户686008

很难知道你具体在问什么,因为你的问题相当模糊(“动画”的意思是什么?“似乎不起作用”是什么意思?解释一下这些术语,因为它们可能意味着多个事情),但我认为你想在点击事件侦听器中将物理体设置为“静态”。在此处引用我所说的内容:

http://developer.anscamobile.com/reference/index/bodybodytype

因此,在selectBubble()函数中,您可以输入类似于tapped.bodyType="static"的内容。

2011-05-20 14:41:39