Modifying display object property in Corona sSk - coronasdk

I am doing my first game in Corona SDK and I've been stuck for a while. I am trying to move two display objects, every object has a position property, after every move I update the position property, the problem is if I click again the object it don't show me the updated position.
I created my objects this way:
while count<=16 do
button[count] = display.newImage(count..".png")
button[count].position = count
count = count+1
end
And in my touch handler listener I want to move object 1 and 2:
local c = 1
while c <= 2 do
--HERE I DO THE MOVEMENT, THIS WORKS
transition.to( button[c], { time=200, delta=true, x=100 } )
--HERE I CHANGE THE POSITION
button[c].position = 1000
c = c+1
end
Then if I click again any of the 2 objects the position property was not updated! Thank you!

Related

Lua value changing by itself?

I'm new to using both Lua and LOVE2D. I thought that a perfect way to get familiar with the tools would be to create a snake style game. currently I have most of the things up and running. I run into a problem when adding to the snake after it eats the food. My snake class currently consists of a table of cells, each cell has a gridX and gridY variable. I also have 2 global variable called lastGridX and lastGridY that keep track of the last position of the last cell in the snake class. I update the position of the cells in reverse order and set them equal to the position of the cell with 1 less index in the table. I update the first cell by just adding a constant value called SCALE, the size of each square on the grid, to it. My problem arises when the position of the position of the first cell is the same as the position of the food. For some reason my lastGridX and lastGridY also change to the x and y positions of the food even though I never change them myself. This makes it so that every cell has the same position which is obviously not what I want to happen. Any help would be appreciated thank you. Here is some of the code below.
function love.update(dt)
-- updates the snake's position in set intervals
moveTimer = moveTimer - dt
if moveTimer <= 0 then
lastGridX = snake[#snake].gridX
lastGridY = snake[#snake].gridY
-- updates the body of the snake
for i = #snake, 2, -1 do
snake[i].gridX = snake[i - 1].gridX
snake[i].gridY = snake[i - 1].gridY
end
-- updates the head of the snake
if direction == "right" then
snake[1].gridX = snake[1].gridX + SCALE
elseif direction == "left" then
snake[1].gridX = snake[1].gridX - SCALE
elseif direction == "up" then
snake[1].gridY = snake[1].gridY - SCALE
elseif direction == "down" then
snake[1].gridY = snake[1].gridY + SCALE
end
moveTimer = MOVE_TIMER_MAX
print(lastGridX, lastGridY)
print(snake[#snake].gridX, snake[#snake].gridY)
if snake[1].gridX == newFood.gridX and snake[1].gridY == newFood.gridY then
-- moves the food to a random location on the grid if it is eaten
newFood.gridX = math.random(0, 63) * SCALE
newFood.gridY = math.random(0, 35) * SCALE
-- adds a new body part to the end of the snake
newC = cell
newC.gridX = lastGridX
newC.gridY = lastGridY
print(lastGridX, lastGridY)
print(snake[#snake].gridX, snake[#snake].gridY)
os.execute("pause")
table.insert(snake, newC)
end
end
end

Two objects collide, one should disappear without affecting the other's position

When object1 collides with object2, I want object2 to disappear without affecting the position or velocity of object1. Object1 is dynamic and object2 is static. So it would be like a bonus object hitting my main object and the bonus object should disappear without affecting anything.
Here is my code for the collision function:
local function onCollision( self,event )
--if my object hits a bonus object
if(event.object2.name == "bonus")then
--self:setLinearVelocity(0,horizontal)
--I have tried the above method but every so often I receive an error
event.object2:removeSelf()
score = score*2
scoreText.text = score --sets the new score
Runtime:removeEventListener("enterFrame", event.object2)
else
composer.gotoScene( "restart" )
Runtime:removeEventListener("touch", onObjectTouch)
end
end
UPDATE:
Try setting your bonus objects as sensors, either in this area, or when you initialize them.
event.object2.isSensor = true
OR
object2.isSensor = true
This should allow Physics & Collision Detection on object2 without any effect on other objects.

Attempt to index global 'player' (a nil value)

I'm getting a (a nil value) error when i try to do this :
player = display.newSprite( imageSheet, "sequenceDataPlayer"..math.random(1, 7) )
Looking at a test print :
print ("sequenceDataPlayer"..math.random(1, 7) )
It prints the data oky 'sequenceDataPlayer1'
What Im i doing wrong here ?
Your print statement is just printing the string "sequenceDataPlayer" concatenated with a random number between 1 and 7.
It took me a little while to figure out how to use sprites in Corona, but here's how I do it. I'll use Player for the variables since that's what you're using.
First I create an options variable to get the frames from my Player.lua file:
optionsPlayer =
{
frames = require("player").frames,
}
Then I create a variable for the image sheet:
playerSheet = graphics.newImageSheet( "player.png", optionsPlayer )
After that, I create a variable to set up the name, the sequence of frames, the time it takes to play, and set how many times it will loop:
spriteOptionsPlayer = { name="Player", start=1, count=10, time=500, loopCount = 1}
Finally, I create the new sprite:
spriteInstancePlayer = display.newSprite( playerSheet, spriteOptionsPlayer )
Once I've done all this, I usually set up the x and y positions, xScale and yScale, and other properties along with adding it to a display group.
Last of all, then I play the sprite somewhere:
spriteInstancePlayer:play()
From what it looks like, you want to have 7 different sprites to choose from. Personally, I would just create seven different sprites using all of the steps above and then put them in a table.
sprites = { spriteInstancePlayer, spriteInstancePlayer2, spriteInstancePlayer3, etc.. }
Then when I wanted to play them, I would set the position and visibility and just do:
r = math,random(1, 7)
sprites[r].x = x position
sprites[r].y = y position
sprites[r].isVisible = true
sprites[r]:play()
Of course, then I would want to set listeners to either completely remove the sprite or set the visibility to false when it's done playing, there's a collision(you'd have to add a physics body and set that all up), or whatever else might happen...
There are probably simpler ways to do it, but that's what I do.
Hope this helps.

In corona sdk how can I reference / specify all indexes of a spawned object on the screen?

Hi I've been wondering this for a while and it's causing me lots of problems not knowing more about how to reference / specify all indexes of a spawned enemy on the screen at one time.
Say when my character dies I want all the enemies on the screen at that time to move away from my dead character as the screen fades out. Simply calling 'enemy1' only makes one (the last spawned I think) do as it's told.
Here is my enemy spawn script:
local spawnTable2 = {}
local function spawnEnemy()
enemy1 = display.newSprite( group, sheetE, sequenceData2 )
enemy1.x=math.random(100,1300)
enemy1.y=math.random(360,760)
enemy1.gravityScale = 0
enemy1:play()
enemy1.type="coin"
enemy1.objTable = spawnTable2
enemy1.index = #enemy1.objTable + 1
enemy1.myName = "enemy" .. enemy1.index
physics.addBody( enemy1, "kinematic",{ density = 0, friction = 0, bounce = 1 })
enemy1.isFixedRotation = true
enemy1.type = "enemy1"
enemy1.timer = nil
enemy1.enterFrame = moveEnemy
Runtime:addEventListener("enterFrame",enemy1)
enemy1.objTable[enemy1.index] = enemy1
hudGroup:toFront()
return enemy1
end
To reference all objects, you would need to have two things.
A Counter
Loop
First make a counter:
counter = 0
Every time the funciton is called have a counter:
counter = counter + 1
For tables, you would do something like this:
spawnTable2[counter].x=math.random(100,1300)
Then when you want to remove the object, you would just do this:
display.remove(spawnTable2[counter])
Just keep in mind, everything you do to manipulate that object will have to be inside that function. Good luck and hope this helps.

How do I reference an image that I have created in an table array in Corona (Lua)?

Apologies for the incredibly noob question, but I'm new to Lua, very very rusty at any code, stuck and can't find the solution!
I'm creating a series of random images on screen using:
for count = 1, 6 do
r = math.random ( 1, 5 )
mpart[count] = display.newImage ("mpart" .. r .. ".png")
mpart[count].y = 680
mpart[count].x = x
mpart[count].spawnednew = false
x = x + 170
mpart[count]:addEventListener ("touch", onTouch)
end
How do I know which object is being touched/moved in the function "onTouch", and how do I add a property to it, e.g.
mpart[1].spawnednew == true
Your onTouch function should have an event parameter passed in. The touched image can then be found by in event.target.
Well first off, lins is spot on about how to reference the touched object: the 'event' parameter of the listener function includes the value 'event.target'
As for adding new data to the touched object, that's as simple as 'event.target.moved = true' and now the object has data at object.moved

Resources