Hi all.
Is it possible to slice an image into several smaller images?
For example, say I have an image that is 200x150 in size, could I cut that into say 10 smaller slices that are 200x15, or would I have to slice it up beforehand in say Fireworks and save out as separate images? [import]uid: 7841 topic_id: 12992 reply_id: 312992[/import]
Hi Appletreeman,
I’ve only been using Corona for a couple of days, but if you wish to ‘blit’ the image so that it becomes animated, you can look at the horse animation tutorial found here.
http://developer.anscamobile.com/content/horse-animation
If however you want to display different sections of the image without animating them then I guess you could use the above code too. I am currently trying to do the latter but am having to resort to the using the animation code and it’s really bloated
I’ll post back here if/when I figure it out. [import]uid: 53021 topic_id: 12992 reply_id: 47661[/import]
You cannot simply “slice up” an image, no. It would need to be comprised of several smaller images which respond to touches or swipes or the like. [import]uid: 52491 topic_id: 12992 reply_id: 47665[/import]
*PS - the Samurai Fruit sample code provides and idea on how to slice objects although not exactly what you are talking about; it might be useful [import]uid: 52491 topic_id: 12992 reply_id: 47666[/import]
You could do something like this:
local spriteSheet = sprite.newSpriteSheet( "largeImage.png", 200, 15 )
You could then get the individual images via the rest of the Sprite API - http://developer.anscamobile.com/reference/index/spritenewsprite [import]uid: 5833 topic_id: 12992 reply_id: 47671[/import]
You can also see the example from Jon, the puzzle example, where an image is split up into irregular shapes. Hope that will give you a better idea.
cheers,
? [import]uid: 3826 topic_id: 12992 reply_id: 47676[/import]
I have a score counter at present that uses text objects but I’d like to replace it with an image. Is it not possible to display selection of the image depending on the couter’s value? Or do I *have* to use 10 separate images in order to achieve this?
- Sorry for the thread hi-jack Appletreeman. [import]uid: 53021 topic_id: 12992 reply_id: 47684[/import]
@rik,
that is in-fact easier, look at the solution provided by Graham above, use a spritesheet to split the image into 10 and you can pick either of them by just setting the frame that you want (which would be a digit in your case)
cheers,
? [import]uid: 3826 topic_id: 12992 reply_id: 47687[/import]
@jayantv. Thanks for the heads up. I went ahead with it and it’s working although now I think I have to create another sprite for when the score reaches double (or even triple) digits! *why didn’t I just stick with a text object!!* doh!
in case anyone wants to see the (messy) code;
[code]
playerScore = 0;
function counterObject()
local counter = sprite.newSpriteSheetFromData( “images/counter.png”, require(“counterSprite”).getSpriteSheetData() )
local spriteSet = sprite.newSpriteSet(counter, 1, 9);
if (playerScore == 0) then
sprite.add(spriteSet, “counterSprite”, 1, 1, 1000, 0);
elseif (playerScore <=9) then
counterSprite:removeSelf();
sprite.add(spriteSet, “counterSprite”, playerScore+1, 1, 1000, 0);
elseif (playerScore >9 ) and (playerScore <100) then
counterSprite:removeSelf();
sprite.add(spriteSet, “counterSprite”, 2, 1, 1000, 0);
–create another sprite for the second digit?
else
end
counterSprite = sprite.newSprite(spriteSet);
counterSprite.x = 24; counterSprite.y = 0;
counterSprite:prepare(“counterSprite”);
counterSprite:play();
end
[/code] [import]uid: 53021 topic_id: 12992 reply_id: 47694[/import]
I’m hoping we will get revamped sprites soon. That’s a lot of work to get a single, changable graphic on the screen.
[import]uid: 19626 topic_id: 12992 reply_id: 47735[/import]
I’ve used this for a simple counter, but now I use custom fonts(ttf).
Code below counts up 100th of a second. Has 3 digits.
Counter Digits:
[code]
local sprite = require(“sprite”)
local counterFrame1, counterFrame2, counterFrame3 = 1, 1, 1
local counterImages = sprite.newSpriteSheet(“timerDigits.png”, 50, 50)
local counterSet = sprite.newSpriteSet(counterImages, 1, 10)
local counterDigit1 = sprite.newSprite(counterSet)
local counterDigit2 = sprite.newSprite(counterSet)
local counterDigit3 = sprite.newSprite(counterSet)
counterDigit1.x = 220; counterDigit1.y = 50
counterDigit2.x = 260; counterDigit2.y = 50
counterDigit3.x = 300; counterDigit3.y = 50
local decimal = display.newRect(277, 69, 6, 6)
local function myTimer()
counterDigit1.currentFrame = counterFrame1
counterDigit2.currentFrame = counterFrame2
counterDigit3.currentFrame = counterFrame3
counterFrame3 = counterFrame3 + 1
if counterFrame3 > 10 then counterFrame3 = 1; counterFrame2 = counterFrame2 + 1 end
if counterFrame2 > 10 then counterFrame2 = 1; counterFrame1 = counterFrame1 + 1 end
timer.performWithDelay(100, myTimer)
end
myTimer()
[/code] [import]uid: 53445 topic_id: 12992 reply_id: 49367[/import]
@Millerszone, you can try creating an array that way you will have lesser redundant code and you can handle as many digits as you want.
and the way to increment the whole set would be, a function
[lua]digits={} – this holds all the digits,
MAX_DIGITS=3 --This holds the number of digits
function updateCounterBy(theNumber, iDigit)
– theNumber is how much you need to increment the counter by, generally this would be 1 to 9 for this example
local i
local theDigit = digits[iDigit].currentFrame
theDigit = theDigit + theNumber
if theDigit > 10 then
if i+1 <= MAX_DIGITS then --Not handling a rollover when it reached 9999 (i.e. MAX)
updateCounter(theDigit-10, i+1)
end
end
end
end[/lua]
Since I am writing this code from the airport lounge, on a computer that does not have corona, I am unsure about the bugs, I can fix this and post the same on my howto.oz-apps.com site. So if you think this is helpful, keep a look out and this shall be included there.
cheers,
? [import]uid: 3826 topic_id: 12992 reply_id: 49378[/import]