Displaying a random image with shake function

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:

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)   

Any help would be awesome! [import]uid: 79620 topic_id: 15800 reply_id: 315800[/import]

@reblewax,
this is how you grease the wheels on this code…

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)  

and if your png’s are names as 1.png, 2.png then you can modify line 6 as

images[i] = display.newImage(i..".png", 110, 176)  

thereby eliminating the need for the shake array

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15800 reply_id: 58361[/import]

Thanks for the reply JayantV.

Can’t seem to get it working :frowning:

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.

[code]

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

[/code] [import]uid: 79620 topic_id: 15800 reply_id: 58367[/import]

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)
[import]uid: 67217 topic_id: 15800 reply_id: 58373[/import]

Ah! Good catch. Still no go on the code though:( [import]uid: 79620 topic_id: 15800 reply_id: 58423[/import]

I’d also like to note that I’m trying to run this on version 2011.591. Not sure if that’s what could be causing errors. [import]uid: 79620 topic_id: 15800 reply_id: 58620[/import]