I'm currently having issues with my code. I'm a total newbie to this stuff so this may be an easy fix but I can't figure it out. I'm trying to have an random image load when the user shakes the iphone. I've managed to get the images to load up but I want the last image loaded to be replaced with the new image. It just keeps stacking the images on top of each other and I can't seem to remove the previous image on the shake function. Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 | local shake = { "1.png", "2.png", "3.png", "4.png" } function shake:accelerometer(e) if (e.isShake == true) then octohedron.isVisible = true transition.from(octohedron, {alpha = 0}) local r = math.random ( 1, 4 ) local imageResult = display.newImage( shake [r], 110, 176) transition.from(imageResult, {alpha = 0}) end end Runtime:addEventListener("accelerometer", shake) |
@reblewax,
this is how you grease the wheels on this code...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | local shake={"...."} local images={} local i for i = 1, #shake do images[i] = display.newImage(shake[i], 110, 176) images.isVisible = false end local function get_images(thisOne) local i for i=1,#images do images[i].isVisible = false end images[thisOne].isvisible = true return images[thisOne] end function shake:accelerometer(e) if (e.isShake == true) then octohedron.isVisible = true transistion.from(octohedron, {alpha=0}) local r = math.random(1,4) local image = get_images[r] transition.from(imageResult,{alpha=0}) end end Runtime:addEventListener("accelerometer", shake) |
Thanks for the reply JayantV.
Can't seem to get it working :(
I'm using the director class from Ricardo Rauber for this app. Would that have any affect on this code? I'll post in the entire code here so you can see.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | module(..., package.seeall) new = function () ------------------ -- Groups ------------------ local localGroup = display.newGroup() ------------------ -- Display Objects ------------------ local background = display.newImage("background.png") local octohedron = display.newImage("octohedron.png") ------------------ -- Inserts ------------------ localGroup:insert ( background ) localGroup:insert ( octohedron ) ------------------ -- Positions ------------------ octohedron.x = 160 octohedron.y = 216 octohedron.isVisible = false local shake = { "1.png", "2.png", "3.png", "4.png" } local images={} local i for i = 1, #shake do images[i] = display.newImage(shake[i], 110, 176) images.isVisible = false images.isVisible = false end local function get_images(thisOne) local i for i=1, #images do images[i].isVisible = false end images[thisOne].isvisible = true return images[thisOne] end function shake:accelerometer(e) if (e.isShake == true) then octohedron.isVisible = true transition.from(octohedron, {alpha = 0}) local r = math.random(1,4) local image = get_images[r] transition.from(imageResult,{alpha=0}) end end Runtime:addEventListener("accelerometer", shake) ------------------ -- MUST return a display.newGroup() ------------------ return localGroup end |
I noticed on line 39 and 40, you have images.isVisible = false -- try changing it to images[i].isVisible = false (and to tidy it up, remove the duplicate from line 40)
Ah! Good catch. Still no go on the code though:(