error loading module '(' expected near '=' - lua

This code is to create function listeners for the cannon shooting. When i run the code, it gave me an error Question1.lua:43 '('expected near '='
function cannonCharge = function(event)
if(event.phase == 'began') then
impulse = 0
cannon.isVisible = true
Runtime:addEventListener('enterFrame', charge)
print ('cannonCharge')
end
end
function shot = function(event)
if(event.phase == 'ended') then
Runtime:removeEventListener('enterFrame', charge)
cannon.isVisible = true
cannon.rotation = 0
cannonBall = display.newImage('cannon ball.png', 84, 220)
physics.addBody(cannonBall, {density = 1, friction = 0, bounce = 0})
cannonBalls:insert(cannonBall)
print ('shot')
-- Shoot cannon ball
cannonBall:applyLinearImpulse(3, impulse, cannonBall.x, cannonBall.y )
--Collision listener
cannonBall:addEventListener ('collision', ballCollision)
end
end
function scene:createScene(event)
...
I added the listeners to enterScene
function scene:enterScene( event )
local group = self.view
background:addEventListener('touch', cannonCharge)
background:addEventListener('touch', shot)
end

Variables don't have types; Only values do. So, instead of
function shot = function(event)
-- ...
end
Try
local shot = function(event)
-- ...
end
If you don't put local, the variable will be global. Use of globals should be minimized.
If you prefer a more structured syntax, you can use:
local function shot(event)
-- ...
end
That's equivalent to:
local shot
shot = function(event)
-- ...
end

You can not assign two touch listener on the same object. Because it's create conflict to which function it's call first.
instead of that, you need to assign one touch and one tap listener. So there is no conflict.
background:addEventListener('tap', cannonCharge)
background:addEventListener('touch', shot)

Related

How to check if a function is in progress in corona sdk

how do I check if a function is in progress? I want it to repeat itself if it isn't already in progress.
local function move(event)
ball.x = 100
ball.y = 200
transition.to(ball, {x=0, y=600, time = 5000})
end
local function check(event)
if( --THE OTHER FUNCTION IS IN PROGRESS)then
--do something
end
end
ball:addEventListener("touch", move)
I haven't used corona, but this is a common javascript idiom and is typically the way you would do it there:
local currentlyMoving = false
local function move(event)
ball.x = 100
ball.y = 200
currentlyMoving = true
transition.to(
ball,
{
x=0,
y=600,
time = 5000,
onComplete = function(obj)
currentlyMoving = false
end
})
end
local function check(event)
if (not currentlyMoving) then
--do something
end
end
ball:addEventListener("touch", move)
You can find more details about the onComplete method here

Touch function not working

local Gin2
local function Gin ( event )
if ( event.phase == "began" ) then
Gin2 = display.newImage("PNGs/Sprite/Gin")
Gin2.x = _H
Gin2.y = _W
end
return true
end
Runtime:addEventListener("touch", Gin )
Hello, so I've been trying to figure out this for some time, but with with no success. So as may guess the idea is to spawn an image by touching.Should I define the object that will be touch?
I have made a code for you to use. When you want to spawn an object via touch listener (Either Runtime event or a single event) you can use event.x and event.y to determine the touch point of the user. Below is the code.
NOTE: I made gin as a array for future use of your spawned object
local val = 1
local gin = {}
local spawnObject = function(event)
if(event.phase == "ended") then
gin[val] = display.newImage("PNGs/Sprite/Gin")
gin[val].x = event.x
gin[val].y = event.y
val = val + 1
end
end
Runtime:addEventListener( "touch", spawnObject )
You are using the 'functional listener' form and for 'touch' event you should not be using Runtime.
In your case, you need to change Runtime to the object that 'is to be touched'. I guess what you want to do is to move the image once it is touched.
so, firstly move
Gin2 = display.newImage("PNGs/Sprite/Gin")
up and out the function. then change Runtime to Gin2.

How to implement this pattern in Lua with Cocos2d-x

I wanna call GameScene setSpecificSquare function in Square, it will occur 'attempt to index field 'squares' error.
In my understanding, require("GameScene") in onTouchEnded(touch event) do not get the last variable of GameScene, it has not been constructed, so it does not have squares attribute.
How to call GameScene static function setSpecificSquare in Square? Or in other word, how to get the last GameScene so I can set self.squares table?
Thanks in advanced!! :)
Square.lua
require "Cocos2d"
require "Cocos2dConstants"
local Square = class("Square",function()
return cc.Node:create()
end)
function Square.create()
local square = Square.new()
return square
end
function Square:ctor()
self.bg = cc.Sprite:create("square_bg.png")
self:addChild(self.bg)
self.isHighlight = true
local function onTouchEnded(touch,event)
--occur error here
require("GameScene"):setSpecificSquare()
end
end
return Square
GameScene.lua
require "Cocos2d"
require "Cocos2dConstants"
local GameScene = class("GameScene",function()
return cc.Scene:create()
end)
function GameScene.create()
local scene = GameScene.new()
return scene
end
function GameScene:ctor()
self.visibleSize = cc.Director:getInstance():getVisibleSize()
self.x = cc.Director:getInstance():getVisibleOrigin().x
self.y = cc.Director:getInstance():getVisibleOrigin().y
self.squares = {}
for i=0, 5 do
self.squares[i] = {} -- create a new row
for j=0, 5 do
self.squares[i][j] = nil
end
end
--add layer
self.bombLayer = cc.Layer:create()
self:addChild(self.bombLayer)
--add squares
self:addSquares()
end
function GameScene:addSquares()
for i=0,5 do
for j=0,5 do
local square = require("src/Square"):create()
square:setPosition(0+j*70,0+i*70)
self.bombLayer:addChild(square)
self.squares[i][j] = square
end
end
end
function GameScene:setSpecificSquare()
--can not access self.square here, occur attempt to index field 'squares' error
self.square[0][0].isHighLight
end
return GameScene
Move self.squares = {} out of ctor function, or call ctor function before calling addSquares, or you can just call this function inside addsquares.
The recommended method is moving it out of function and creating it on creation of your GameScene object:
require "Cocos2d"
require "Cocos2dConstants"
local GameScene = class("GameScene",function()
return cc.Scene:create()
end)
function GameScene.create()
local scene = GameScene.new()
return scene
end
GameScene.squares = {}
...

Corona Sdk - Is there a way to call and group:insert() an object from another lua file?

As stated in the title I would like to not only call an object from an external Lua file, but I would also like to group:insert() this object into my Menu page with the properties given to it in the external lua file. Is this possible and/or efficient? I would just really like to make sure data isn't repeated through out my project.
EDIT
Here's my code so far:
The group:insert() function is throwing me an error stating it was expecting a table and that I might have been trying to call a function in which case i should use ":" instead of "."
This is menu.lua:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local widget = require "widget"
local m = require ("myData")
local menuFunction = require("menuFunction")
local menuSwipe
-- =======================
-- menuSwipe()
-- =======================
menuSwipe = function(self, event)
local phase = event.phase
local touchID = event.id
if(phase == "began") then
elseif(phase == "moved") then
elseif(phase == "ended" or phase == "cancelled") then
if(m.menuActivator > 0) then
menuDown(m.invisiBar, event)
else
--m.layerInfo = layers
transition.to( menuFunction.menuBar, { x = menuFunction.menuBar.x, y = 0, time = 200 } )
--transition.to( layers, { x = menuFunction.menuBar.x, y = h, time = 100 } )
m.invisiBar = display.newRect( 0,0,w,25,6)
m.invisiBar.alpha = 0
m.menuActivator = 1
end
end
end
-- ++++++++++++++++++++++
-- menuDown()
-- ++++++++++++++++++++++
function menuDown(self, event)
local phase = event.phase
local touchID = event.id
if(phase == "began") then
elseif(phase == "moved") then
elseif(phase == "ended" or phase == "cancelled") then
if(m.menuActivator == 1) then
transition.to( menuFunction.menuBar, { x = m.menuInfo.x, y = h*.964, time = 200 } )
--transition.to( group, { x = 0, y = 0, time = 10 } )
m.menuActivator = 0
end
end
end
function scene:createScene( event )
local group = self.view
group:insert( menuFunction.menuBar ) -- *** ERROR occurs here
end
function scene:enterScene( event )
local group = self.view
end
function scene:exitScene( event )
local group = self.view
end
function scene:destroyScene( event )
local group = self.view
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
This is menuFunction.lua:
local m = require("myData")
local menu = require ("menu")
local w = display.contentWidth
local h = display.contentHeight
local menuFunction = {}
--menuBar
menuFunction.menuBar = display.newImage( "images/menuBar1.png")
menuFunction.menuBar.x = w*(1/2)
menuFunction.menuBar.y = h*1.465
menuFunction.menuBar.height = h
menuFunction.menuBar:setReferencePoint(display.TopLeftReferencePoint)
menuFunction.menuBar.touch = menu.menuSwipe
menuFunction.menuBar:addEventListener("touch", menuFunction.menuBar)
return menuFunction
This is the exact error message:
ERROR: table expected. If this is a function call, you might have used '.' instead of ':'
message**
Does this happen every time this code is called, or does it by any chance work the first time and then crashes? In your case, code could work the first time you enter the scene, but the second time you do, it may crash [if you remove scenes in between].
When you do a 'require' of a file, its contents are executed and returned value is saved in the global packages table. When you require the same file again, the returned value is taken from the global packages table instead, the code is not executed again.
So if you by any chance require this file in one spot of your app, and then call :removeSelf() and nil the reference of the menuBar, the display object will be removed and its reference will cease to exist, and calling the require again, will not recreate the object. Fully removing a scene will also remove the display objects.
So what you wanted to achieve is very sensible [contrary to what #Schollii says], but your "module" should allow creation of multiple objects if you want to get rid of them during runtime.
I'm not going to correct your code, just a simple example of how you can achieve this:
-- menu.lua
local menuCreator = {}
menuCreator.newMenu = function(params)
local menu = display.newGroup()
-- create your menu here
return menu
end
return menuCreator
Now anytime you do:
local menuCreator = require("menu.lua")
you will be able to call:
local menu = menuCreator.newMenu(someParams)
and get yourself a nice new menu wherever you need.
If it's not shown all the time on screen, it may be better to create a new one whenever you need it, and then remove it from the memory.
There are several issues with this, and none of them seem related to your error but fixing them will either also fix the error or make the cause of the error more obvious. Please fix following and update:
Although Lua allows it, don't use circular includes, where A includes B which includes A. Instead have menu require menuFunction and then call a creation function in menuFuntion:
-- menuFunction.lua
local m = require("myData")
-- require("menu") -- BAD! :)
local w = display.contentWidth
local h = display.contentHeight
local menuBar = display.newImage( "images/menuBar1.png")
menuBar.x = w*(1/2)
menuBar.y = h*1.465
menuBar.height = h
menuBar:setReferencePoint(display.TopLeftReferencePoint)
local menuFunction = { menuBar = menuBar }
function createMenuBar(menuSwipe)
menuFunction.menuBar.touch = menuSwipe
menuFunction.menuBar:addEventListener("touch", menuFunction.menuBar)
return menuFunction
end
-- menu.lua
function createScene(event)
local mf = require('menuFunction')
mfFunction = mf.createMenuBar(menuSwipe)
group:insert(menuFunction.menuBar)
end
Secondly out of the four calls to group:insert() the first 3 refer to objects that are not shown in the code and don't see relevant to problem, they should be removed or if you think relevant, comment why their code now shown, or show their code.

Images doesn´t appear in storyboard Corona SDK

I am doing a game in corona SDK, but I have this little problem.
I have a menu with a button. If I press it, it sends me to the first level of my game.
When I pass the final level, the game return me to the menu. Bur, if I start playing the first again, my images doesn´t appear.
The images are balls, and to pass the level, you have to eliminate all the balls. To do this, I use:
ball:removeSlef()
ball = nil
But, I don´t think that this is the problem, because I eliminate this lines, and it doesn´t work.
The images are create in scene:createScene function, and insert in the Group.
I short the code of the first level to be understood.
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local physics = require "physics"
physics.start(); physics.pause()
physics.setGravity( 0, 0 )
local cont = 0
local bur = {}
function eliminar1( event )
if (cont == 0) and (event.phase == "began") then
event.target:removeSelf()
bur[1] = nil
cont = cont + 1
end
end
function eliminar2( event )
if (cont == 1) and (event.phase == "began") then
bur[2]:removeSelf()
bur[2] = nil
cont = cont + 1
end
end
function eliminar3( event )
if (cont == 2) and (event.phase == "began") then
bur[3]:removeSelf()
bur[3] = nil
storyboard.gotoScene( "levels.1.level2" )
end
end
function scene:createScene ( event )
local screenGroup = self.view
for i = 1,3 do
bur[i] = display.newImage("irudiak/"..i..".png")
bur[i]:translate(math.random(0,280), math.random(0,400) )
physics.addBody( bur[i], {bounce = 0.3 } )
bur[i]:setLinearVelocity(math.random(-50,50), math.random(-50,50) )
screenGroup:insert(bur[i])
end
bur[1]:addEventListener("touch", eliminar1)
bur[2]:addEventListener("touch", eliminar2)
bur[3]:addEventListener("touch", eliminar3)
end
function scene:enterScene( event )
local screenGroup = self.view
physics.start()
end
function scene:exitScene( event )
local screenGroup = self.view
physics.stop()
end
function scene:destroyScene( event )
local screenGroup = self.view
package.loaded[physics] = nil
physics = nil
end
return scene
createScene is ran only first time when you gotoScene. Every next time only willEnterScene and enterScene are played. To play createScene again you have to remove it (storyboard.removeScene() I guess). Or you can move some stuff you need to willEnterScene. For more detailed info you can watch this: http://www.coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Resources