Stumped Again... Trying to switch objects at touch repeatedly.

I’m trying to switch objects whenever that particular object is touched. For example, my background has the number “1” written on it. When you touch the “1” it will be removed, and the number “2” will show up in its place. Then when you touch the number “2”, the number “3” will show up in it’s place, and so on. I achieved getting the “1” to be replaced by “2”, but then trying to switch to “3” it creates an error. Below is my code that I’m using. I’m pretty sure I know what’s wrong, but I don’t know how to make it right. I’m a newbie and I’m digging this stuff, but I still got a lot to learn. Any help is appreciated.

module (…, package.seeall)

function new()

local level1Group = display.newGroup()

local background = display.newImage( “images/Background1.png”, 0, 0 )
local number1 = display.newImage(“images/1.png”)
number1.x = 85
number1.y = 275

level1Group:insert(background)
level1Group:insert(number1)

function rectlistener()

number1:removeEventListener(“touch”, rectlistener )
number1:removeSelf()
number2 = display.newImage( “images/2.png” )
number2.x = 85; number2.y = 275

end

number1:addEventListener(“touch”, rectlistener )

function rectlistener()

number2:removeEventListener(“touch”, rectlistener )
number2:removeSelf()
number3 = display.newImage( “images/3.png” )
number3.x = 85; number3.y = 275

end

number2:addEventListener(“touch”, rectlistener )

return level1Group

end
[import]uid: 138755 topic_id: 24301 reply_id: 324301[/import]

Hey, try this code;

[lua]require “sprite”

local bg = display.newRect( 0, 0, 320, 480 )

numberSheet = sprite.newSpriteSheet(“test.png”, 14, 18)

numberSet = sprite.newSpriteSet(numberSheet, 1, 3)
sprite.add(numberSet, “numbers”, 1, 3, 1000, 0)

numbers = sprite.newSprite(numberSet)
numbers.x, numbers.y = 100, 100

local function countUp()
if numbers.currentFrame < 3 then
numbers.currentFrame = numbers.currentFrame + 1
end
end
numbers:addEventListener(“tap”, countUp)[/lua]

Using this image: http://puu.sh/nGJb

Also please post your code in < lua > and < /lua > tags, makes it readable :wink:

Peach :slight_smile: [import]uid: 52491 topic_id: 24301 reply_id: 98183[/import]

Thanks Peach. I plugged in the code, but I’m getting this error:

…/user/Desktop/LearnWithRoccoNumbers/LearnNumbers.lua:15: bad argument #1 to ‘newSpriteSet’ (sprite.SpriteSheet expected, got nil)

Also, sorry about not posting the code in [lua], but I actually can"t figure out how to do that either. All I’ve been doing is copying and pasting into the post. What’s the proper way? Sorry again for being such newbie. :wink: [import]uid: 138755 topic_id: 24301 reply_id: 98188[/import]

I figured out the error. I have my images in a a folder actually called “images”. I changed that and no more error, but all I’m getting is a grey square with no interactivity or numbers. [import]uid: 138755 topic_id: 24301 reply_id: 98199[/import]

Try in a new folder, new project, paste that code into main.lua and put the image I linked to in the folder. (Make sure it is named test.png.)

That should work fine but let me know, if you are still struggling I’ll upload the whole thing as a zip for you.

For code, it’s easy, like this;

< lua >
Write your code here
< /lua >

Without the spaces, obviously.

Get it? :slight_smile: [import]uid: 52491 topic_id: 24301 reply_id: 98213[/import]

Yeah, it works fine in the new folder. I have my project orientation set to landscape so that’s why it was displaced. So, are you suggesting I create a sprite sheet with all of the numbers I want on it? If so, I’ll give it a shot, but it will be my first time attempting that. Wish me luck! [import]uid: 138755 topic_id: 24301 reply_id: 98294[/import]

Yes that is my suggestion; at first it will seem difficult but very, very quickly it will become easy.

Trust me, once you understand it you will find it SO useful.

If you need help there’s TexturePacker - it has a free version which may help. (Worth buying, though.)

Peach :slight_smile: [import]uid: 52491 topic_id: 24301 reply_id: 98422[/import]

Thanks for the suggestion. I’ll look into TexturePacker if needed. [import]uid: 138755 topic_id: 24301 reply_id: 98445[/import]