swaping images

I’ve read lots of posts and looked at lot’s of generic code but as usual, I think I could understand it better if I had some code specific to my situation.
I have objects that spawn. These spawned objects are the same size/shape and all have the same physics. The only differences between them are the colors.
ex. square-red.png, square-orange.png, square-blue.png
etc…about 8- 10 different colors.

what i would like to happen is that if I click on a certain object (square-red.png) and then click on a different object (square-blue.png) then the two objects switch places. This would have to be available with any of the spawned objects…does that make sense?

here’s a snippit of my code:

local diamondGfx = { “square-yellow.png”, “square-orange.png”, “square-blue.png”, “square-red.png”,
“square-aqua.png”, “square-green.png”, “square-purple.png”, “square-white.png” }
------------------------------------------------ table for storing objects created in TABLE diamondGfx
local alldiamonds = {}
------------------------------------------------------------new spawn diamond
local function spawndiamond( )

– audio.play( popSound )
randImage = diamondGfx[math.random( 1, 8 )]
alldiamonds[#alldiamonds + 1] = display.newImage( randImage )
local diamond = alldiamonds[#alldiamonds]
diamond.x = 50 – could be randomised?
diamond.y = 100 – could be randomised?
physics.addBody( diamond, { density=0.0, friction=0.0, bounce=0.2 } )—add radius=00.8 specify collision spot. middle (not outer edge) of object is collision

---------------listen at diamond(s) for a touch. If there is then go to event “local function dragBody (event)”
diamond:addEventListener( “touch”, dragBody )
-------------------------------------------------------------------------needs to be within “local function spawndiamond” to work
local diamondmove = diamond----------------------------------------------CODE TO MOVE DIAMONDS FROM ONE POINT TO ANOTHER
physics.addBody( diamondmove, { density=1.5, friction=0.6, radius=00.3, bounce=0.3 } )
diamondmove.x=40
diamondmove.y=60 [import]uid: 127842 topic_id: 22570 reply_id: 322570[/import]

This is not neat and needs some cleaning up but here is a rough example;

[lua]local obj1

local square = display.newRect( 60, 60, 50, 50 )

local circle = display.newCircle( 200, 80, 30 )

local function swap (event)
if obj1 == nil then
obj1 = event.target
elseif obj1 then
obj2 = event.target
moveToX, moveToY = obj2.x, obj2.y
event.target.x, event.target.y = obj1.x, obj1.y
obj1.x, obj1.y = moveToX, moveToY
obj1, obj2 = nil, nil
end
end
square:addEventListener(“tap”, swap)
circle:addEventListener(“tap”, swap)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 22570 reply_id: 90122[/import]

Thanks peach.
Everyone here has been great at answering newbie questions.

Speaking of questions…(this is my version of a transition:) )

Is there a way to have a number of spawned images (25) line up 5 high and 5 deep? (5x5 grid). Right now I just have them spawning with some gravity effects to make them bounce around and “sort of” line up on there own but it isn’t what I’m looking for…
Any help appreciated. [import]uid: 127842 topic_id: 22570 reply_id: 90176[/import]

[lua]local row = 1; local col = 1

local function spawndiamond()

– audio.play( popSound )
randImage = diamondGfx[math.random( 1, 8 )]
alldiamonds[#alldiamonds + 1] = display.newImage( randImage )

local diamond = alldiamonds[#alldiamonds]

local xPos = 50 + (col * 50) – adjust to get spacing right
local yPos = 50 + (row * 50) – adjust to get spacing right
diamond.x = xPos
diamond.y = yPos

physics.addBody( diamond, { density=0.0, friction=0.0, bounce=0.2 } )—add radius=00.8 specify collision spot. middle (not outer edge) of object is collision

col = col + 1
if col == 6 then
col = 1
row = row + 1
end

end

for a = 1, 25, 1 do
spawndiamond()
end[/lua] [import]uid: 93133 topic_id: 22570 reply_id: 90180[/import]

Thanks nick! [import]uid: 127842 topic_id: 22570 reply_id: 90185[/import]

generic question:
code spacing:
what’s the difference between these two same lines of code.
one uses “proper”(?) spacing and the other has one line of code under the other.

local diamond = alldiamonds[#alldiamonds]

local xPos = 50 + (col * 50) – adjust to get spacing right
local yPos = 50 + (row * 50) – adjust to get spacing right

diamond.x = xPos
diamond.y = yPos

and:
local diamond = alldiamonds[#alldiamonds]
local xPos = 50 + (col * 50) – adjust to get spacing right
local yPos = 50 + (row * 50) – adjust to get spacing right
diamond.x = xPos
diamond.y = yPos
[import]uid: 127842 topic_id: 22570 reply_id: 90187[/import]

Nothing - I just find it’s just a lot easier to read when you come back to it later on if different blocks of code have gaps in between.

Also, finally at the age of 30 I have started to indent my code properly after being lazy and stubborn about it until now, it really does help, especially in lua where it’s really easy to put too many or not enough ‘end’ statements.

The comment I put about spacing was in reference to the ‘50’ values, you can adjust these to put the diamonds closer together or further away, and also how far from the left/top of the screen the block of diamonds starts. [import]uid: 93133 topic_id: 22570 reply_id: 90194[/import]

ok. Thanks.

I tried the code but it slows to a crawl almost to the point of locking up and then everything kinds just pops into view but not in any order…I’ll have to dink with it. [import]uid: 127842 topic_id: 22570 reply_id: 90196[/import]

There shouldn’t be anything in the example I posted that would lock up.

I’m confused by your code at the top, what is the ‘diamondmove’ bit for? It looks like you are adding a physics body to each diamond, then setting up ‘diamondmove’ to point at the diamond, and adding another physics body to the same object.
[import]uid: 93133 topic_id: 22570 reply_id: 90199[/import]

‘diamondmove’ is some code that I recieved from someone and I modified it a little.
It basically (from what i understand) let’s me click on a diamond and then click onto a blank area of the screen and the diamond moves there.

I’m trying to figure out how I can replace different diamonds by: example: clicking on one diamond (diamond-red.png) and then on a different diamond (diamond-green.png) and have them swap places. [import]uid: 127842 topic_id: 22570 reply_id: 90200[/import]

Sorry nick. I can’t get that code to work without it totally slowing down to a crawl [import]uid: 127842 topic_id: 22570 reply_id: 90207[/import]

Without knowing what other code is conflicting with it it’s difficult to help, my section of code should work fine in isolation.

The diamondmove code looks wrong to me, at present it’s just adding a second physics body to each diamond and moving them all to x=40, y = 60. Whatever it does I don’t think it should be within the spawndiamond function.

Feel free to send your code to otterstudios@hotmail.co.uk and I’ll have a look.

However, I think you need to really understand the basics (loops, functions, tables, events, collisions etc) - you could build a game from all the help on here but if you don’t know why large parts of it do what they do, it’s difficult to fix as it gets more complex and things go wrong.

When I first got Corona 5 months ago I didn’t have a clue, but learnt everything I needed to know dissecting and tinkering with the ‘ghosts v monsters’ sample code. Once I knew how it worked, within a few days I had turned the whole thing round so that the player had to build the tower to protect the monsters and the CPU fired the ghosts. Only then was I ready to start building my own games.

[import]uid: 93133 topic_id: 22570 reply_id: 90225[/import]

The diamondmove code did have some extra code that didn’t belong.

physics.addBody and some x-y coordinates
[import]uid: 127842 topic_id: 22570 reply_id: 90250[/import]

Yea, I’m not attempting to build a game per se. More like taking different code and messing around with it trying to figure out what it does.
It’s just seems easier (for me) if i have something I’m trying to figure out that’s specific to my questions rather than dissecting someone else’s code and trying to figure out their methods. or getting generic code examples. My brain seems to understand better if I can apply it to my own code.Does that make sense?
[import]uid: 127842 topic_id: 22570 reply_id: 90227[/import]