You need to read the sprite documentation from start to finish, and implement it one line at a time.
Copying and pasting sections of code in from here, into seemingly random places, isn’t going to work.
For example, your sheetOptions table is doing nothing there, coming after you’re trying to create the sprite. That needs to be run before the newImageSheet command and passed in as the second argument.
and I only got the image showing and it’s barely on the screen . It doesn’t move or anything . It’s just at the top left corner all I can see it a leg . This is the code :
local options = { -- Required parameters width = 50, height = 41, numFrames = 2, -- Optional parameters; used for scaled content support sheetContentWidth = 70, -- width of original 1x size of entire sheet sheetContentHeight = 82 -- height of original 1x size of entire sheet } local imageSheet = graphics.newImageSheet( "images3.png", options ) -- sequences table local character = { -- consecutive frames sequence { name = "normalRun", start = 1, count = 8, time = 800, loopCount = 0, loopDirection = "forward" } } -- consecutive frames local sequenceData = { name="walking", start=3, count=6, time=100, loopCount = 0, -- Optional ; default is 0 (loop indefinitely) loopDirection = "bounce" -- Optional ; values include "forward" or "bounce" } local character = display.newSprite( imageSheet, sequenceData ) -- sprite listener function local function spriteListener( event ) local thisSprite = event.target -- "event.target" references the sprite if ( event.phase == "ended" ) then thisSprite:setSequence( "fastRun" ) -- switch to "fastRun" sequence thisSprite:play() -- play the new sequence end end
character:addEventListener( "sprite", spriteListener )
This kind of helps . When I run the code it shows but I only see the legs in a medium sized square on the screen I don’t know how to fix this . My guess is that maybe it’s running that way because your using composer and i’m using storyboard . I’ll try to fix the code until I get an answer . My code :
local storyboard = require( "storyboard" ) local scene = storyboard.newScene() local physics = require "physics" physics.start() -- background -------------------------------------------- local character -- forward declarations and other locals local options = { width = 100, height = 125, numFrames = 20, sheetContentWidth = 500, sheetContentHeight = 500 } local sheet = graphics.newImageSheet("images3.png", options) local sequence = { name = "walk", start = 11, count = 10, time = 750, loopCount = 0 --for infinite looping } local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX
function scene:createScene(event) local screenGroup = self.view local background = display.newImageRect("images.png",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) character = display.newSprite(sheet, sequence) character.x = 100 character.y = 300 character:scale(-1, 1) character:play() end
function scene:enterScene(event) Runtime:addEventListener("collision", onCollision) gun:addEventListener( "touch", shootBullet ) if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen transition.to(character, {x = display.screenOriginX, time = 4000, onComplete = function(self) if (character.isPlaying == true) then character:pause() end end}) -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. physics.start() end end
function scene:exitScene(event) Runtime:removeEventListener("collision", onCollision) if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) physics.stop() elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroyScene(event) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view package.loaded[physics] = nil physics = nil end
IF you are using your own image do not use my options table nor my sequence table. Also, quick question, is the GIF working for you? I just want to know if Corona supports this. I do not want to draw attention away from your question.
Also, please upload your image so we can help you with the options table.
Ok, the problem I see here is that the individual frames have to be spread out in an imageSheet, (don’t quote my on this) but, I think that’s the only way. Try splitting up the GIF into frames into an image sheet.
The point is, all images for the same sequence need to be on a single-layer png. Imagine the letters below (A - L) are frames in an animation - you would lay out your spritesheet in the same way.
A B C
D E F
G H I
J K L
Then you need to define your options table based on how big each frame is and the gaps between each frame. The values will be unique to your file so can’t just be copied from here, otherwise you will just get fragments of a frame and a jumbled mess.
Highly recommend texture packer. What it does is take individual images and position them neatly as a single image, ready to use in Corona. It will even generate the lua code for you so you don’t have to calculate the options table.
I’d also recommend buying a few assets from graphic river to practice with. For as little as $9 you can have a complete character, including all the typical animations you’d need like walk, jump, punch, die, slide etc. Put those into texture packer and you’re away.