hey guy i am new to corona and i am trying to spawn some objects that fall out of the sky on to the ground. i have posted this same code already the person who answered don't answer the question fully. i need help with this so i could get my game on track thanks guys. some one please tell me why i am getting "Attempt to index field "?" (nil value)" i know something doesn't exist. also the code that is giving me this problem is "object[objectTag].x = 30+mRandom(320)" i try to comment it out and try to work without it but the error goes to the next line. can someone help. thanks
local mRandom = math.random
local objects = {"rocket02" ,"rocket01","coin01"}
local objectTag = 0
local object = {}
local function spawnObject()
objectTag = objectTag + 1
local objIdx = mRandom(#objects)
local objName = objects[objIdx]
object[objectTag] = display.newImage("image/object_"..objName..".png")
*object[objectTag].x = 30+mRandom(320)
object[objectTag].y = 200
object[objectTag].name = objectTag*
print(objectTag)
end
timer.performWithDelay(1,spawnObject,3)
Can you use this code block to see if there is a problem with image loading
local mRandom = math.random
local objects = {"rocket02" ,"rocket01","coin01"}
local objectTag = 0
local object = {}
local function spawnObject()
objectTag = objectTag + 1
local objIdx = mRandom(#objects)
local objName = objects[objIdx]
object[objectTag] = display.newImage("image/object_"..objName..".png")
if object[objectTag] ~= nil then
print( "Image succesfully loaded." )
else
print( "Image couldn't be found." )
end
*object[objectTag].x = 30+mRandom(320)
object[objectTag].y = 200
object[objectTag].name = objectTag*
print(objectTag)
end
timer.performWithDelay(1,spawnObject,3)
Related
I want to be able to pass the value from a stepper widget in corona into a json file. So far I've got my stepper to display and increment and decrement and display the current value on screen. I've written a function to save the value to a file as json, but i'm having trouble calling that function. I want to be able to have other setting on screen which have their values save to json. Here the code I have to save the setting:
local function saveSettings (e)
-- body
print("testtesttest")
if (file) then
local content = file:read("*a")
else
settings = {}
settings.houses = StepperNo
file = io.open(path, "w")
local json_setting = json.encode(settings)
file:write(json_setting)
io.close(file)
end
end
and then the code to display the stepper:
local StepperNo = 1
local displayNoOfHouses = display.newText("Number of houses: "..StepperNo,180,135, native.systemFontBold,16)
local function onStepperPress( e )
if ("increment" == e.phase) then
StepperNo = StepperNo + 1
elseif ("decrement") == e.phase then
StepperNo = StepperNo - 1
end
displayNoOfHouses.text = "Number of houses: "..StepperNo
end
local HouseStepper = widget.newStepper {left = 100, top = 100, minimumValue = 0, maximumValue = 9, onPress = onStepperPress}
I dont know how to call the savesetting function after the stepper is pressed. I think I might have done this incorrectly or may be missing something obvious, I am very new to Lua...
There is a very simple to implement table saver and loader presented in this tutorial:
http://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/
It uses JSON to encode a Lua table to save and when needed, read the JSON back in and decode it to a Lua table.
This function reads a file line by line and sets global variables to the values read accordingly. However, the problem is that this code works on the simulator 100% fine however once compiled and run on the android device it stops working. Through the use of displaying text I have located the error to be on the line of code: "mapObject[i].x = sum". The attribute .x is of type real and I've already confirmed that sum, numerator and denominator are all numerical data types. The app crashes on the first run through of the loop containing "mapObject[i].x = sum" so i = 1. 1 is within range of the array. Any help is appreciated, I just can't get my head around this. Here is the function code:
-- Read map from file
local function loadmap(mapnum)
local tempstring
local numerator
local denominator
local sum
local i
local path = system.pathForFile( "maps/map" .. mapnum .. ".txt", system.ResourceDirectory)
--Open the file
local fh = io.open( path, "r" )
if fh then
-- read the lines of data systematically.
mapName = fh:read( "*l" )
mapNPCS = fh:read( "*l" )
mapSpawnTimer = tonumber(fh:read( "*l" ))
mapSpawnMax = tonumber(fh:read( "*l" ))
mapSpawnMaxLocations = tonumber(fh:read( "*l" ))
mapObjectMax = tonumber(fh:read( "*l" ))
for i = 1, mapObjectMax do
tempstring = fh:read( "*l" )
mapObject[i] = display.newImage("graphics/" .. tempstring .. ".png")
-- Fractional mapping for all screen sizes
denominator = tonumber(fh:read( "*l" ))
numerator = tonumber(fh:read( "*l" ))
sum = (display.contentWidth / denominator) * numerator
mapObject[i].x = sum
denominator = tonumber(fh:read( "*l" ))
numerator = tonumber(fh:read( "*l" ))
sum = (display.contentHeight / denominator) * numerator
mapObject[i].y = sum
mapObject[i].myName = "object"
physics.addBody(mapObject[i], "kinematic", {density = 10.0, friction = 0.0})
end
for i = 1, mapSpawnMaxLocations do
mapSpawnX[i] = tonumber(fh:read( "*l" ))
mapSpawnY[i] = tonumber(fh:read( "*l" ))
end
io.close( fh )
end
end
here is the global variables that come with the code:
-- Map Data Variables
local mapName
local mapNPCS
local mapSpawnTimer
local mapSpawnMax
local mapSpawnMaxLocations
local mapObjectMax
local mapObject = {}
local mapSpawnX = {}
local mapSpawnY = {}
-- Map Npc Spawning
local mapNPC = {}
local maxMapNPCS = 30
local npcsSpawned
local spawnTimer = 0
Some things to try on physical device:
If you use sum = display.contentWidth does it work?
How about sum = 100?
Could it be that the denominator is 0? (although that shouldn't cause a crash, just an error)
Try replacing mapObject[i].x = sum by mapObject[i].x = 100, does it still crash?
could it be that mapObject[i] is not a valid display object, say if the path in mapObject[i] = display.newImage("graphics/" .. tempstring .. ".png") is not valid or if tempstring is empty? (although you'd think display.newImage('graphics/.png') should not cause any trouble)
I'm going to guess that:
mapObject[i] = display.newImage("graphics/" .. tempstring .. ".png")
is producing a file name that's invalid on the device. Device's are case sensitive, where as the simulator is not.
hey i am new to the Corona sdk world i want to learn how to spawn some objects and make them move across the screen i try everything and it never work i read the forum on spawning the right way and try it but still end up with a error in my code help this is my code
local mRandom = math.random
local mAbs = math.abs
local objects = {"rocket02" ,"rocket01","coin01"}
local function spawnObject()
local objIdx = mRandom(#objects)
local objName = objects[objIdx]
local object = display.newImage("image/object_"..objName..".png")
object.x = mRandom (screenLeft +30,screenRight-30)
object.y = screenTop
if objIdx < 4 then
object.type = "food"
else
object.type = "other"
end
end
Also can some one tell me how to make it move across the screen
Please help Thanks
here's the media file for you to take a look at
I'll show you a method. For that, I have rewritten your code as follows:
local mRandom = math.random
local objects = {"rocket02" ,"rocket01","coin01"}
local objectTag = 0
local object = {}
local function spawnObject()
objectTag = objectTag + 1
local objIdx = mRandom(#objects)
local objName = objects[objIdx]
object[objectTag] = display.newImage(objName..".png") -- see the difference here
object[objectTag].x = 30+mRandom(320)
object[objectTag].y = 200
object[objectTag].name = objectTag
print(objectTag)
end
timer.performWithDelay(1,spawnObject,3)
Here I've used a timer for displaying the object. You can also use for loop for the same purpose.
Here you can call any object with tag as object[objectTag].
For your information:
display.newImage(objName..".png")
--[[ will display object named rocket02.png or rocket01.png or coin01.png
placed in the same folder where your main.lua resides --]]
And
display.newImage("image/object_"..objName..".png")
--[[ will display object named object_rocket02.png or object_rocket01.png
or object_coin01.png placed in a folder named 'image'. And the folder
'image' should reside in the same folder where your main.lua is. --]]
And for moving your object from top to bottom, you can use:
either
function moveDown()
object[objectTag].y = object[objectTag].y + 10
--replace 'objectTag' in above line with desired number (ir., 1 or 2 or 3)
end
timer.performWithDelay(100,moveDown,-1)
or
transition.to(object[objectTag],{time=1000,y=480})
--[[ replace 'objectTag' in above line with desired number (ir., 1 or 2 or 3)
eg: transition.to(object[1],{time=1000,y=480}) --]]
Keep coding.............. :)
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I'm getting the error:
Runtime error
...n-stylr\documents\corona projects\happy_day\game.lau:50: attempt to index global 'city1' (a nil value)
My code is as follows:
function scene:createScene(event)
local screenGroup = self.view
local background = display.newImage("background1.jpg")
local city1 = display.newImage("bild.png")
city1:setReferencePoint(display.BottomLeftReferencePoint)
city1.x = 0
city1.y = 640
city1.speed = 1
local city2 = display.newImage("bild.png")
city2:setReferencePoint(display.BottomLeftReferencePoint)
city2.x = 1136
city2.y = 640
city2.speed = 1
local city3 = display.newImage("build2.png")
city3:setReferencePoint(display.BottomLeftReferencePoint)
city3.x = 0
city3.y = 640
city3.speed = 2
local city4 = display.newImage("build2.png")
city4:setReferencePoint(display.BottomLeftReferencePoint)
city4.x = 1136
city4.y = 640
city4.speed = 2
end
function scene:enterScene(event)
Runtime:addEventListener("touch", touchScreen)
print(city1)
city1.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city1)
city2.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city2)
city3.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city3)
city4.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", city4)
end
Just declarelocal city1 outside the createScene function and create city1 as:
local city1;
function scene:createScene(event)
-- your code for localGroup and bg
city1 = display.newImage("bild.png") -- just create city1 as this
-- do the rest of your code
end
If the error occurs reffering other cities(city2 ,3 or 4), do the same methode for all those.
Keep coding................ :)
I'm working on a project that required to save signature(Image Base 64 ). Currently it's working fine but sometimes around 2% out of 100% failed due to unknown reason.
$(document).ready(function() {
var db = window.openDatabase("TEST", "1.0", "TEST", 20000000 );
db.transaction(insertSignature, errorCB, insertSignatureSuccess);
function insertSignature(tx)
{
var signature = $.trim($('#sig').val());
var signature_laps = $('#signature_laps').val();
var signature_attempts = $('#signature_attempts').val();
var sql = 'UPDATE signature_table SET signature = "'+signature+'", signature_lap = "'+signature_laps+'", signature_attempt = "'+signature_attempts+'", modified_date = datetime("now", "localtime")' +
' WHERE cust_code = "'+cust_code+'" AND cycle_month = "'+month+'" AND cycle_year = "'+year+'"';
tx.executeSql(sql);
}
function insertSignatureSuccess(tx)
{
alert('success');
}
function errorCB(err)
{
alert('failed');
}
});
WHERE
signature variable contains base64 image
signature_laps & signature_attempts variable contains integers
cust_code, month & year are just important key parameters
ON SQLITE
signature field is BLOB
On 2% out of 100%, the 2% failure still says success even though the signature is not save but when i tried again with same stroke it successfully save. Why is it still proceeding on success message even the query is not successfully executed? It supposed to proceed on failed message. Is it a phonegap bug?
I'm running the phonegap under android ICS and jquery mobile.
Any idea? thanks guys in advance.
I don't know if it's the cause of the error or not, but with SQL commands u better state them as follows:
var sql='UPDATE signature_table SET signature = ?, signature_lap = ?, ' +
'signature_attempt = ?, modified_date = datetime("now", "localtime") ' +
'WHERE cust_code = ? AND cycle_month = ? AND cycle_year = ?';
tx.executeSql(sql, [signature, signature_laps, signature_attempts, cust_code, month, year]);
Also, seen this situation, it seems advisable to put the success- and callback functions in the transaction:
tx.executeSql(sql, [signature, signature_laps, signature_attempts, cust_code, month, year], insertSignatureSuccess, errorCB);