在 Corona APK 中为图像添加监听器
2020-3-19 9:58:15
收藏:0
阅读:127
评论:1
我在 CoronaLabs Studio 中创建了一个新项目,并选择了“基于物理的游戏”选项,所有内容都作为初学者加载,但我不知道应该在哪里使用 addEventListener() 来让方块可以点击或在点击时被销毁。
我尝试了很多不同的方式,只是把这一行代码放在脚本中让盒子可点击 virus:applyLinearImpulse( 0, -0.25, virus.x, virus.y )
以下是 level1.lua 脚本。
在这种情况下,我有
require("toast")
local composer = require( "composer" )
local scene = composer.newScene()
-- 加入 Corona 的“物理”库
local physics = require("physics")
--------------------------------------------
tapCount = 0
tapText = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40 )
--------------------------------------------
local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX
function scene:create( event )
-- 当场景的视图不存在时调用。
-- 在此处插入代码以初始化场景,例如将显示对象添加到 'sceneGroup'、添加触摸监听器等。
local sceneGroup = self.view
physics.start()
physics.pause()
local background = display.newImageRect("game-background.png", 1170, 658)
background.x = display.contentCenterX
background.y = display.contentCenterY
-- OFFSCREEN BOX,设置位置并略微旋转
local box = display.newImageRect( "box.png", 40, 40 )
box.x, box.y = 160, -100
box.rotation = 33
-- 添加物理效果
physics.addBody( box, { density=1.0, friction=0.3, bounce=0.2 } )
-- 创建一个草地对象并添加物理(使用自定义形状)
local grass = display.newImageRect( "grass.png", screenW, 82 )
grass.anchorX = 0
grass.anchorY = 1
-- 在屏幕底部绘制草地
grass.x, grass.y = display.screenOriginX, display.actualContentHeight + display.screenOriginY
local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }
physics.addBody( grass, "static", { friction=0.3, shape=grassShape } )
sceneGroup:insert( background )
sceneGroup:insert( grass )
sceneGroup:insert( box )
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
-- 当场景仍未在屏幕上时调用
elseif phase == "did" then
-- 当场景现在在屏幕上时调用
physics.start()
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == "will" then
-- Called when the scene is on-screen and is about to move off-screen
physics.stop()
elseif phase == "did" then
-- Called when the scene is now off-screen
end
end
function scene:destroy( event )
-- Called prior to the removal of scene's "view" (sceneGroup)
local sceneGroup = self.view
package.loaded[physics] = nil
physics = nil
end
---------------------------------------------------------------------------------
-- 监听器设置
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-----------------------------------------------------------------------------------------
return scene
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

如果我正确理解了你的问题,这里是一个例子,你可以点击一个方框并摧毁它。
function scene:create(event) local function destroyMe(event) display.remove(event.target) event.target=nil end local box = display.newImageRect( "box.png", 40, 40 ) box.x, box.y = 160, 100 box.rotation = 33 -- add physics physics.addBody( box, { density=1.0, friction=0.3, bounce=0.2 } ) box:addEventListener("tap",destroyMe) end如果你正在使用
touch事件,那么那个event将会有三个阶段。因此要小心。