One more try. Swapping images

I’ve tried messing with different code and trying to incorporate it into my own code but nothing has worked. I’ve tried countless examples and I’ve always run into a road block.

Everyone who has tried to help me is appreciated.
BUT I cannot get past 2 annoying problems.

#1. I cannot get the 2 objects (square and circle) to swap.Clicking on a square then clicking on a circle should swap them around.
#2. How can I get 2 or 3 circles to remove themselves if they are touching each other? My circles are individual images all with the same dimensions and physics. Color is the only difference between them.
ANY help would be appreciated.

local squareGfx = { “circle-white-bomb.png”, “circle-red-bomb.png” }
local allsquares = {}

local circleGfx = { “circle-white.png”, “circle-orange.png”, “circle-blue.png”, “circle-red.png”,
“circle-green.png”, “circle-purple.png”, “circle-grey.png” }
local allcircles = {}

local function spawnsquare( )
randImage = squareGfx[math.random( 1, 2 )]
allsquares[#allsquares + 1] = display.newImage( randImage )
local square = allsquares[#allsquares]
square.x = 35 – could be randomised?
square.y = 93 – could be randomised?
physics.addBody( square, { density=0.0, friction=0.0, bounce=0.0, radius = 31 } )

square:addEventListener( “touch”, dragBody )

end
timer.performWithDelay(interval, spawnsquare, 5)
local function spawncircle( )

– audio.play( popSound )
randImage = circleGfx[math.random( 1, 8 )]
allcircles[#allcircles + 1] = display.newImage( randImage )
local circle = allcircles[#allcircles]
circle.x = 50 – could be randomised?
circle.y = 375 – could be randomised?
physics.addBody( circle, { density=-5.0, friction=0.0, bounce=-2.0, radius = 27 } )

circle:addEventListener( “touch”, dragBody )

end

timer.performWithDelay(interval, spawncircle, 25) [import]uid: 127842 topic_id: 22738 reply_id: 322738[/import]

This should do it, replaces your dragBody function if you had one and uses two new variables, ‘state’ and ‘tempObj’ to determine whether or not a first object has been touched, and store a pointer to that object.
[lua]local squareGfx = { “circle-white-bomb.png”, “circle-red-bomb.png” }
local allsquares = {}
local circleGfx = { “circle-white.png”, “circle-orange.png”, “circle-blue.png”, “circle-red.png”,
“circle-green.png”, “circle-purple.png”, “circle-grey.png” }
local allcircles = {}
local state = 1 – 1 = nothing touched, 2 = first object touched
local tempObj – store first object in here

local function dragBody(event)
local obj = event.target
if event.phase == “ended” then

if state == 1 then
tempObj = obj
tempObj.alpha = 0.7 – fade slightly to show is clicked
state = 2
else
local moveX = obj.x – store 2nd object’s position before moving it
local moveY = obj.y
transition.to(obj, {time = 500, x = tempObj.x, y = tempObj.y, transition=easing.inOutExpo}) – move object two to object one’s position
transition.to(tempObj, {time = 500, alpha = 1, x = moveX, y = moveY, transition=easing.inOutExpo}) – move object one to object two’s original position
state = 1
end
end
end

local function spawnsquare( )
randImage = squareGfx[math.random( 1, 2 )]
allsquares[#allsquares + 1] = display.newImage( randImage )
local square = allsquares[#allsquares]
square.x = 35 – could be randomised?
square.y = 93 – could be randomised?
square.id = #allsquares

physics.addBody( square, { density=0.0, friction=0.0, bounce=0.0, radius = 31 } )
square:addEventListener( “touch”, dragBody )
end

timer.performWithDelay(interval, spawnsquare, 5)
local function spawncircle( )
– audio.play( popSound )
randImage = circleGfx[math.random( 1, 8 )]
allcircles[#allcircles + 1] = display.newImage( randImage )
local circle = allcircles[#allcircles]
circle.x = 50 – could be randomised?
circle.y = 375 – could be randomised?
circle.id = #allcircles
physics.addBody( circle, { density=-5.0, friction=0.0, bounce=-2.0, radius = 27 } )
circle:addEventListener( “touch”, dragBody )
end
timer.performWithDelay(interval, spawncircle, 25)[/lua] [import]uid: 93133 topic_id: 22738 reply_id: 90731[/import]

nick!
That’s exactly what I’m looking for. You are a God
…and the comments are going to make things much clearer.

I added some other code that you gave me for 25 spawned images. I never get a consistent number of spawned images.
I’ve tried 2 different ways but neither seem to work.
I’ve tried adding some if then statements but nothing stays consistent.

#1. Your code:
col = col + 1
if col == 6 then
col = 1
row = row + 1
end

end

for a = 1, 30, 1 do
spawncircle()
end

#2. Generic Code
timer.performWithDelay(interval, spawncircle, 1)

[import]uid: 127842 topic_id: 22738 reply_id: 90741[/import]

You didn’t add all the necessary code:
[lua]local row = 1; local col = 1

local function spawncircle( )
– audio.play( popSound )
randImage = circleGfx[math.random( 1, 8 )]
allcircles[#allcircles + 1] = display.newImage( randImage )
local circle = allcircles[#allcircles]
local xPos = 50 + (col * 50) – adjust to get spacing right
local yPos = 50 + (row * 50) – adjust to get spacing right
circle.x = xPos
circle.y = yPos
circle.id = #allcircles
col = col + 1
if col == 6 then
col = 1
row = row + 1
end
physics.addBody( circle, { density=-5.0, friction=0.0, bounce=-2.0, radius = 27 } )
circle:addEventListener( “touch”, dragBody )
end[/lua]

Then you can either generate all 25 circles in one go:

[lua]for a = 1, 30, 1 do
spawncircle()
end[/lua]

Or generate them gradually using the timer:

[lua]timer.performWithDelay(interval, spawncircle, 25)[/lua] [import]uid: 93133 topic_id: 22738 reply_id: 90756[/import]

Thank You! [import]uid: 127842 topic_id: 22738 reply_id: 90759[/import]

hey nick, hate to be a pest but no matter what i try i cannot get the objects to spawn exactly 25. Sometimes it will but most times not.
I played around with some of the code and can pretty much manipulate all of it except the dang 25 spawned items.

I also don’t understand these lines. I can manipulate them but not sure what (col * 50) means. I was thinking it was the farthest distance before creating a new row but it doesn’t seem to be true.
and how does tempObj and obj work? How does it know that it is referring to my spawned squares and spawned circles. Is it because there are really only 2 distinct objects on the screen?
(square and circle)?
[import]uid: 127842 topic_id: 22738 reply_id: 90782[/import]

‘Col’ stores the column number for the next circle to be created.

So when col = 1, it will be positioned on the X axis at 50 + (1*50) which is 100. When it is 5, it will be 50 + (5*50) or 300. If you changed the (col*50) part, the distance between the circles would change.

After each circle is created col is increased by 1. When it equals 6, we want to start a new row so row becomes 2, and col is reset back to one.

We manipulate tempobj and obj within the dragBody function. This is only fired when a circle or square is touched.

local obj = event.target

Event.target will be whatever circle or square you clicked on. Local variable obj is used to refer to event.target. As it’s a local variable within the dragBody function, when that function ends it will cease to exist. It just means you don’t have to type event.target everytime you want to refer to the clicked object, and also makes the code a little faster.

Anything you do to obj will happen to the object that was clicked on.
tempobj is used when the first object is clicked, so the program remembers what object was clicked. When you click on a second object, obj will be the second object and tempobj is the first object, so we can then read their positions and swap them.

[import]uid: 93133 topic_id: 22738 reply_id: 90934[/import]

Thanks nick. I appreciate the explanation.
Now any idea why I cannot get 25 spawned items? They keep spawning between 21 and 25 but never exact. I’ve tried both of these but it never works correctly. I can’t find anywhere else where there would be interference but I see nothing.

for a = 1, 25, 1 do
spawncircle()
end

timer.performWithDelay(interval, spawncircle, 25)

Update:
It is kicking out 25 of them but some of them are disappearing almost immediately. I didn’t notice until I slowed it down. [import]uid: 127842 topic_id: 22738 reply_id: 90941[/import]

Is the issue that it won’t spawn 25 circles or all 25 different types of circle ? [import]uid: 84637 topic_id: 22738 reply_id: 90946[/import]

25 circles [import]uid: 127842 topic_id: 22738 reply_id: 90950[/import]

…and Danny. I have a couple more questions. Nick has been great but any help is appreciated.

Looking at my above code, can you tell me how I would make all the objects that are touching each other with the same colors remove themselves? You think assigning numbers to the circles would work?
(not that I know how to do that…)There are probably going to be about 15 different colored circles. [import]uid: 127842 topic_id: 22738 reply_id: 90956[/import]

Ok so first off, i would be more inclined to do the spawn function like this :

[code]
local row = 1; local col = 1
math.randomseed(os.time()) --If you don;t add this it wont be truly random

local function spawncircle( )
– audio.play( popSound )
randImage = circleGfx[math.random( 1, 8 )]

local circle = display.newImage( randImage )
local xPos = 50 + (col * 50) – adjust to get spacing right
local yPos = 50 + (row * 50) – adjust to get spacing right
circle.x = xPos
circle.y = yPos
circle.id = #allcircles
col = col + 1
if col == 6 then
col = 1
row = row + 1
end
physics.addBody( circle, { density=-5.0, friction=0.0, bounce=-2.0, radius = 27 } )
circle:addEventListener( “touch”, dragBody )

allcircles[#allcircles + 1] = circle
end [import]uid: 84637 topic_id: 22738 reply_id: 90959[/import]

Can’t seem to get that to work. I’ll play around with it. I may have changed some things since asking for help.

Any ideas about removing multiple objects based on thier color? [import]uid: 127842 topic_id: 22738 reply_id: 90964[/import]