Lua中的赋值混淆
我正在分析科罗纳样本代码中的鱼类项目,我无法理解这个任务Lua中的赋值混淆,lua,variable-assignment,coronasdk,Lua,Variable Assignment,Coronasdk,我正在分析科罗纳样本代码中的鱼类项目,我无法理解这个任务 background = ( backgroundLandscape == background and backgroundPortrait ) or backgroundLandscape 以下是完整的代码: -- Seed randomizer local seed = os.time(); math.randomseed( seed ) display.setStatusBar( display.HiddenStatusBa
background = ( backgroundLandscape == background and backgroundPortrait ) or backgroundLandscape
以下是完整的代码:
-- Seed randomizer
local seed = os.time();
math.randomseed( seed )
display.setStatusBar( display.HiddenStatusBar )
-- Preload the sound file (theoretically, we should also dispose of it when we are completely done with it)
local soundID = audio.loadSound( "bubble_strong_wav.wav" )
-- Background
local halfW = display.viewableContentWidth / 2
local halfH = display.viewableContentHeight / 2
-- Create a table to store all the fish and register this table as the
-- "enterFrame" listener to animate all the fish.
local bounceAnimation = {
container = display.newRect( 0, 0, display.viewableContentWidth, display.viewableContentHeight ),
reflectX = true,
}
local backgroundPortrait = display.newImage( "aquariumbackgroundIPhone.jpg", 0, 0 )
local backgroundLandscape = display.newImage( "aquariumbackgroundIPhoneLandscape.jpg", -80, 80 )
backgroundLandscape.isVisible = false
local background = backgroundPortrait
-- Handle changes in orientation for the background images
local backgroundOrientation = function( event )
-- TODO: This requires some setup, i.e. the landscape needs to be centered
-- Need to add a centering operation. For now, the position is hard coded
local delta = event.delta
if ( delta ~= 0 ) then
local rotateParams = { rotation=-delta, time=500, delta=true }
if ( delta == 90 or delta == -90 ) then
local src = background
-- toggle background to refer to correct dst
background = ( backgroundLandscape == background and backgroundPortrait ) or backgroundLandscape
background.rotation = src.rotation
transition.dissolve( src, background )
transition.to( src, rotateParams )
else
assert( 180 == delta or -180 == delta )
end
transition.to( background, rotateParams )
audio.play( soundID ) -- play preloaded sound file
end
end
-- Add a global listener
Runtime:addEventListener( "orientation", backgroundOrientation )
--
-- Fishies
local numFish = 10
local file1 = "fish.small.red.png"
local file2 = "fish.small.blue.png"
--
-- Define touch listener for fish so that fish can behave like buttons.
-- The listener will receive an 'event' argument containing a "target" property
-- corresponding to the object that was the target of the interaction.
-- This eliminates closure overhead (i.e. the need to reference non-local variables )
local buttonListener = function( event )
if "ended" == event.phase then
local group = event.target
-- tap only triggers change from original to different color
local topObject = group[1]
if ( topObject.isVisible ) then
local bottomObject = group[2]
-- Dissolve to bottomObject (different color)
transition.dissolve( topObject, bottomObject, 500 )
-- Restore after some random delay
transition.dissolve( bottomObject, topObject, 500, math.random( 3000, 10000 ) )
end
-- we handled it so return true to stop propagation
return true
end
end
--
--
--
-- Add fish to the screen
for i=1,numFish do
-- create group which will represent our fish, storing both images (file1 and file2)
local group = display.newGroup()
local fishOriginal = display.newImage( file1 )
group:insert( fishOriginal, true ) -- accessed in buttonListener as group[1]
local fishDifferent = display.newImage( file2 )
group:insert( fishDifferent, true ) -- accessed in buttonListener as group[2]
fishDifferent.isVisible = false -- make file2 invisible
-- move to random position in a 200x200 region in the middle of the screen
group:translate( halfW + math.random( -100, 100 ), halfH + math.random( -100, 100 ) )
-- connect buttonListener. touching the fish will cause it to change to file2's image
group:addEventListener( "touch", buttonListener )
-- assign each fish a random velocity
group.vx = math.random( 1, 5 )
group.vy = math.random( -2, 2 )
-- add fish to animation group so that it will bounce
bounceAnimation[ #bounceAnimation + 1 ] = group
end
--
-- Function to animate all the fish
function bounceAnimation:enterFrame( event )
local container = self.container
container:setFillColor( 0, 0, 0, 0) -- make invisible
local containerBounds = container.contentBounds
local xMin = containerBounds.xMin
local xMax = containerBounds.xMax
local yMin = containerBounds.yMin
local yMax = containerBounds.yMax
local orientation = self.currentOrientation
local isLandscape = "landscapeLeft" == orientation or "landscapeRight" == orientation
local reflectX = nil ~= self.reflectX
local reflectY = nil ~= self.reflectY
-- the fish groups are stored in integer arrays, so iterate through all the
-- integer arrays
for i,v in ipairs( self ) do
local object = v -- the display object to animate, e.g. the fish group
local vx = object.vx
local vy = object.vy
if ( isLandscape ) then
if ( "landscapeLeft" == orientation ) then
local vxOld = vx
vx = -vy
vy = -vxOld
elseif ( "landscapeRight" == orientation ) then
local vxOld = vx
vx = vy
vy = vxOld
end
elseif ( "portraitUpsideDown" == orientation ) then
vx = -vx
vy = -vy
end
-- TODO: for now, time is measured in frames instead of seconds...
local dx = vx
local dy = vy
local bounds = object.contentBounds
local flipX = false
local flipY = false
if (bounds.xMax + dx) > xMax then
flipX = true
dx = xMax - bounds.xMax
elseif (bounds.xMin + dx) < xMin then
flipX = true
dx = xMin - bounds.xMin
end
if (bounds.yMax + dy) > yMax then
flipY = true
dy = yMax - bounds.yMax
elseif (bounds.yMin + dy) < yMin then
flipY = true
dy = yMin - bounds.yMin
end
if ( isLandscape ) then flipX,flipY = flipY,flipX end
if ( flipX ) then
object.vx = -object.vx
if ( reflectX ) then object:scale( -1, 1 ) end
end
if ( flipY ) then
object.vy = -object.vy
if ( reflectY ) then object:scale( 1, -1 ) end
end
object:translate( dx, dy )
end
end
-- Handle orientation of the fish
function bounceAnimation:orientation( event )
print( "bounceAnimation" )
for k,v in pairs( event ) do
print( " " .. tostring( k ) .. "(" .. tostring( v ) .. ")" )
end
if ( event.delta ~= 0 ) then
local rotateParameters = { rotation = -event.delta, time=500, delta=true }
Runtime:removeEventListener( "enterFrame", self )
self.currentOrientation = event.type
for i,object in ipairs( self ) do
transition.to( object, rotateParameters )
end
local function resume(event)
Runtime:addEventListener( "enterFrame", self )
end
timer.performWithDelay( 500, resume )
end
end
Runtime:addEventListener( "enterFrame", bounceAnimation );
Runtime:addEventListener( "orientation", bounceAnimation )
-- This function is never called,
-- but shows how we would unload the sound if we wanted to
function unloadSound()
audio.dispose(soundID)
soundID = nil
end
——种子随机化器
本地种子=os.time();
数学.随机种子(种子)
display.setStatusBar(display.HiddenStatusBar)
--预加载声音文件(理论上,当我们完全处理完它时,我们也应该处理它)
本地soundID=audio.loadSound(“气泡\u strong\u wav.wav”)
--背景
本地halfW=display.viewableContentWidth/2
local halfH=display.viewableContentHeight/2
--创建一个表来存储所有的鱼,并将此表注册为
--“enterFrame”侦听器为所有鱼设置动画。
本地反弹图像={
container=display.newRect(0,0,display.viewableContentWidth,display.viewableContentHeight),
reflectX=真,
}
本地BackgroundGrait=display.newImage(“backgroundiphone.jpg”,0,0)
localbackgroundscape=display.newImage(“boundraundiphonescape.jpg”、-80、80)
backgroundscape.isVisible=false
本地背景=背景肖像
--处理背景图像方向的更改
本地背景方向=功能(事件)
--TODO:这需要一些设置,即景观需要居中
--需要添加定心操作。目前,这个位置是硬编码的
本地增量=event.delta
如果(δ~=0),则
局部旋转图={rotation=-delta,time=500,delta=true}
如果(δ==90或δ==-90),则
本地src=背景
--切换背景以参考正确的dst
背景=(背景景观==背景和背景肖像)或背景景观
background.rotation=src.rotation
转换。溶解(src,背景)
过渡到(src,旋转图)
其他的
断言(180==增量或-180==增量)
结束
过渡到(背景、旋转图)
播放(soundID)--播放预加载的声音文件
结束
结束
--添加一个全局侦听器
运行时:addEventListener(“方向”,backgroundOrientation)
--
--鱼
本地numFish=10
local file1=“fish.small.red.png”
local file2=“fish.small.blue.png”
--
--为fish定义touch listener,使fish可以像按钮一样工作。
--侦听器将收到一个包含“target”属性的“event”参数
--对应于作为交互目标的对象。
--这消除了闭包开销(即需要引用非局部变量)
本地按钮列表器=函数(事件)
如果“结束”==event.phase,则
本地组=event.target
--点击仅触发从原始颜色到不同颜色的更改
局部拓扑对象=组[1]
如果(toObject.isVisible),则
本地对象=组[2]
--溶解到底部对象(不同颜色)
变换.分解(拓扑对象,底部对象,500)
--在一些随机延迟后恢复
分解(底部对象,拓扑对象,500,数学随机(3000,10000))
结束
--我们处理了它,因此返回true以停止传播
返回真值
结束
结束
--
--
--
--在屏幕上添加鱼
对于i=1,numFish do
--创建代表我们的鱼的组,存储两个图像(file1和file2)
本地组=display.newGroup()
本地fishOriginal=display.newImage(文件1)
组:插入(fishOriginal,true)--在buttonListener中作为组[1]访问
本地fishDifferent=display.newImage(文件2)
组:插入(fishDifferent,true)--作为组[2]在buttonListener中访问
fishDifferent.isVisible=false--使文件2不可见
——在屏幕中间的一个200×200区域中移动到随机位置
组:translate(halfW+math.random(-100100),halfH+math.random(-100100))
--连接按钮列表器。触摸鱼会使其更改为file2的图像
组:addEventListener(“触摸”,按钮Listener)
--为每条鱼指定一个随机速度
group.vx=数学随机(1,5)
group.vy=math.random(-2,2)
--将鱼添加到动画组,使其反弹
bounceAnimation[#bounceAnimation+1]=组
结束
--
--函数设置所有鱼的动画
函数bounceAnimation:enterFrame(事件)
本地容器=self.container
容器:setFillColor(0,0,0,0)--使不可见
本地containerBounds=container.contentBounds
本地xMin=containerBounds.xMin
本地xMax=containerBounds.xMax
本地yMin=containerBounds.yMin
本地yMax=containerBounds.yMax
本地方向=自当前方向
本地isLandscape=“landscapeLeft”==方向或“landscapeRight”==方向
局部反射x=nil~=self.reflectX
局部反射=零~=自反射
--fish组存储在整数数组中,因此遍历所有
--整数数组
对于ipairs(self)do中的i,v
local object=v——要设置动画的显示对象,例如鱼群
本地vx=object.vx
局部vy=object.vy
如果(isLandscape)那么
如果(“景观左”==方向),则
本地vxOld=vx
vx=-vy
vy=-vxOld
elseif(“景观权”==方向)然后
本地vxOld=vx
vx=vy
vy=vxOld
结束
elseif(“肖像向上向下”==方向)然后
vx=-vx
vy=-vy
结束
--TODO:现在,时间是以帧而不是秒来衡量的。。。
局部dx=vx
局部dy=vy
局部边界=object.contentBounds
本地flipX=错误
本地flipY=false
如果(bounds.xMax+dx)>xMax,则
flipX=真
dx=xMax-bounds.xMax
elseif(bounds.xMin+dx)yMax,则
flipY=true
dy=yMax-bounds.yMax
elseif(bounds.yMin+dy)background = ( backgroundLandscape == background and backgroundPortrait ) or backgroundLandscape
if backgroundLandscape == background then
background = backgroundPortrait
else
background = backgroundLandscape
end
background = backgroundLandscape