shuffling images in a table - lua

I am trying to shuffle the table of image and I also want to store the co ordinates for those images how can i do that? Is there any other way to do that? The one that I have done is this
local alpha = {{"alpha_a"} , {"alpha_b"} , {"alpha_c"} , {"alpha_d"} ,
{"alpha_e"} , {"alpha_f"} , {"alpha_g"} , {"alpha_h"}}
local coordinates ={{x=092, y=470}, {x=197, y=470}, {x=302, y=470},
{x=407, y=470}, {x=512, y=470}, {x=617, y=470} }
for i=1, #alpha do
local selection = table.remove(coordinates, math.random(#coordinates))
print(selection.x,selection.y, #coordinates)
images = display.newImage(alpha[i][1]..".png")
images.x = selection.x
images.y = selection.y
images:addEventListener("touch",swapping)
end

It's not entirely clear what you are trying to achieve, but I guess it might be keeping the image together with its own coordinates.
I think the logical approach to accomplishing this is reconsidering your data structure, and putting the coordinates and names into the same table like
local alpha = {{"alpha_a",x=092, y=470} , {"alpha_b",x=197, y=470} , {"alpha_c",x=302, y=470} , {"alpha_d",x=407, y=470} , {"alpha_e",x=512, y=470}} --...

Related

How to update a variable outside a function in corona sdk?

How do I change the variable outside a function(in my case it is an eventListener)? I have a goal, which is a circle and I want its radius to change when the score is over 10. How do I update the variable to check what the radius is?
local goal
local goalRadius = 40
local function checkScore()
if(score>10)then
goalRadius = 20
--UPDATE THE VARIABLE?
end
goal = display.newCircle(0,0,goalRadius)
goal:addEventListener("touch", checkScore)
What I did to solve this proble was this: I used removeSelf() to delete the goal and then I recreated it.
local goal
local goalRadius = 40
local function checkScore()
if(score>10)then
goal:removeSelf()
goalRadius = 20
goal = display.newCircle(0,0,goalRadius)
end
end
goal = display.newCircle(0,0,goalRadius)
goal:addEventListener("touch", checkScore)
If you need change radius of circle use goal.width and goal.height. Just remember that goal.width == goal.height == two radii for circle.

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.

How to change the position of display objects which are inside a table

I want to create a table with display Objects on specific positions. I wrote the following
for i=0, 5 do
life[i] = display.newImage( "life.png" )
end
but when I am trying this:
for i=0, 5 do
life[i] = display.newImage( "life.png" )
life[i].x=i*Space_
end
compiler complains attemp to index field ? nil value
Any idea why this happen or how can I solve it? I want to use a for loop to add objects in a table (or maybe group? ) on specific positions.
Show us where Space_ is in your code.
Try this:
local SpaceX = 10
for i=0, 5 do
life[i] = display.newImage( "life.png" )
life[i].x = i * SpaceX
end
Corona doesn't find your image and returns nil as the result of display.newImage call. When you try then to access field x of a nil value, you get the error.
Make sure your image is available to your script.
This assert should probably trigger, because life.png could not be found
for i=0, 5 do
life[i] = assert(display.newImage("life.png"), "image could not be found")
life[i].x=i*Space_
end

Display screenshot after it is taken

I am trying to display a screenCap after it is taken, it is saving the screenCap but how can i get the latest screenCap's url ?
local screenCap = display.captureScreen( true )
local alert = native.showAlert( "Success", "Screen Capture Saved to Library", { "OK" } )
NewsScreenShot = display.newImage( " path to the PNG file " )
http://jp.anscamobile.com/dev/reference/index/displaycapturescreen/index.html
The picture will be saved in the device gallery with a name Picture X.png. After this you need to custom selection to the latest indexed picture.
Btw you can try it whit display.save("name",path) and this will be alway the last saved picture.
display.captureBounds is good for saving the whole screen to the directory. But it usually saves the file with increase in last index. So it may be difficult to read them correctly. So I prefer display.save. But it is not a straight way.
For doing this, you have to:
First create a displayGroup.
Then add the screen objects to that group.
Return the display group.
Use display.save to save the entire group displayed.
Display the desired image from system.DocumentsDirectory.
I am giving a sample here:
-- creating the display group --
local localGroup = display.newGroup()
-- creating display objects and adding it to the group --
local bg = display.newRect(0,0,_w,_h)
bg.x = 160
bg.y = 240
bg:setFillColor(150)
localGroup:insert(bg)
local rect = display.newRect(0,0,50,50)
rect.x = 30+math.random(260)
rect.y = 30+math.random(420)
localGroup:insert(rect)
-- Take Screenshot --
local function saveGroupImages()
-- take screen shot to baseDirectory --
local baseDir = system.DocumentsDirectory
display.save( localGroup, "myScreenshot.jpg", baseDir )
end
rect:addEventListener("tap",saveGroupImages)
After this, you can read the file and display it as follows:
local readImage = display.newImage( "myScreenshot.jpg" ,system.DocumentsDirectory , 50, 100 )
readImage.x = 160
readImage.y = 240
readImage:scale(0.5,0.5)
keep Coding........... :)

Combine images into single image

How can I combine two PNGs into one image?
If one image is displays "1", and another image displays "9" - I'd like make an image file showing "19".
There is a object "group" which group the images into an array, but it doesn't seem that I can merge the members of a group.
number1 = display.newImage( "number1.png" );
number9 = display.newImage( "number9.png" );
number19 = display.newGroup();
number19:insert(number1)
number19:insert(number9)
--Put 9 next to 1
number9.left =number1.width
I use a snapshot when I need to combine images. Very similar to a group, the snapshot can be rendered out as a single PNG or JPEG.
local Function MakeSnapshot()
local snapshot = display.newSnapshot(digitWidth*2, digitWidth)
local digit1 = display.newImage("number1.png")
local digit2 = display.newImage("number9.png")
digit1:translate(-digitWidth/2, 0)
digit2:translate(digitWidth/2, 0)
snapshot.group:insert(digit1)
snapshot.group:insert(digit2)
snapshot:invalidate()
--Save file as a single image:
display.save(snapshot, "19.png", system.DocumentsDirectory)
end

Resources