0
votes

I'm getting a (a nil value) error when i try to do this :

player = display.newSprite( imageSheet, "sequenceDataPlayer"..math.random(1, 7) )

Looking at a test print :

print ("sequenceDataPlayer"..math.random(1, 7) )

It prints the data oky 'sequenceDataPlayer1'

What Im i doing wrong here ?

1
player = display.newSprite(imageSheet, {name = "sequenceDataPlayer"..math.random(1, 7)}) - Egor Skriptunoff
hi egor that seems to crash the game - kevin ver

1 Answers

0
votes

Your print statement is just printing the string "sequenceDataPlayer" concatenated with a random number between 1 and 7.

It took me a little while to figure out how to use sprites in Corona, but here's how I do it. I'll use Player for the variables since that's what you're using.

First I create an options variable to get the frames from my Player.lua file:

optionsPlayer =
        {
            frames = require("player").frames,
        }

Then I create a variable for the image sheet:

playerSheet = graphics.newImageSheet( "player.png", optionsPlayer )

After that, I create a variable to set up the name, the sequence of frames, the time it takes to play, and set how many times it will loop:

spriteOptionsPlayer = { name="Player", start=1, count=10, time=500, loopCount = 1}

Finally, I create the new sprite:

spriteInstancePlayer = display.newSprite( playerSheet, spriteOptionsPlayer )

Once I've done all this, I usually set up the x and y positions, xScale and yScale, and other properties along with adding it to a display group.

Last of all, then I play the sprite somewhere:

spriteInstancePlayer:play()

From what it looks like, you want to have 7 different sprites to choose from. Personally, I would just create seven different sprites using all of the steps above and then put them in a table.

sprites = { spriteInstancePlayer, spriteInstancePlayer2, spriteInstancePlayer3, etc.. }

Then when I wanted to play them, I would set the position and visibility and just do:

r = math,random(1, 7)
sprites[r].x = x position
sprites[r].y = y position
sprites[r].isVisible = true
sprites[r]:play() 

Of course, then I would want to set listeners to either completely remove the sprite or set the visibility to false when it's done playing, there's a collision(you'd have to add a physics body and set that all up), or whatever else might happen...

There are probably simpler ways to do it, but that's what I do. Hope this helps.