Unable to create a working sprite - lua

I am unable to create a working animation sprite for this single area of my app.
I am using Corona SDK and have the following sprite:
This is names mainCharacter.png. I have a double sized version called mainCharacter#2x.png.
I have sheet options, 2 sequences, I'm building an image sheet and passing that to my sprite:
local playerSheetOptions =
{
width = 50,
height = 50,
numFrames = 17,
sheetContentWidth = 500,
sheetContentHeight = 100
}
local playerSequences = {
{
name = "idle",
start = 1,
count = 12,
time = 1200,
loopCount = 0,
loopDirection = "bounce"
},
{
name = "jump",
start = 13,
count = 5,
time = 600,
loopCount = 1
},
}
local playerSheet = graphics.newImageSheet( "resource/images/mainCharacter.png", playerSheetOptions )
local player = display.newSprite(gameSheet, playerSheet, playerSequences)
I am getting the following error:
display.newSprite() requires argument #2 to a table containing sequence data
If I print the relevant data:
print(gameSheet)
print(playerSheet)
print(playerSequences)
I get:
14:27:05.703 userdata: 12445228
14:27:05.703 userdata: 0CF42600
14:27:05.703 table: 0CF41FD0
Where am I going wrong? I have tried simplifying the sequences a lot, but still get the same thing.

Use
local player = display.newSprite(playerSheet, playerSequences)
instead of
local player = display.newSprite(gameSheet, playerSheet, playerSequences)
From Corona documentation
Once the image sheet(s) and sequences are set up, a new sprite object
can be created with the display.newSprite() API:
display.newSprite( [parent,] imageSheet, sequenceData )
For this API, the parent
parameter is optional and represents the display group in which to
insert the sprite. The imageSheet parameter defines the default image
sheet for the sprite, and sequenceData is the table that contains all
of the sequences for the sprite.
Read more about Sprite Animation.

Related

MapView to background - Corona SDK

I need some help with the MapView in Corona SDK. I want to put the map as background (but with all functions) and, in this case, a button to the front. Here is my code:
function scene:createScene( event )
local group = self.view
if ( system.getInfo( "environment" ) == "simulator" ) then
local simulatorMessage = "Maps not supported in Corona Simulator.\nYou must build for iOS or Android to test native.newMapView() support."
local label = display.newText( simulatorMessage, 36, 40, 270, 0, native.systemFont, 12 )
label.anchorX = 0
label.anchorY = 0
end
myMap = native.newMapView( 20, 20, display.contentWidth, display.contentHeight )
if ( myMap ) then
myMap.mapType = "normal"
myMap.x = display.contentCenterX
myMap.y = display.contentCenterY
myMap:setCenter( currentLatitude, currentLongitude )
myMap:addEventListener("mapLocation", mapLocationListener)
end
backBtn = widget.newButton
{
id="backButton",
label="Back",
fontSize="30",
labelColor={ default={1, 1, 1}, over={0, 0, 0, 0.5} },
defaultFile="images/button.png",
width = dispWidth * 0.25, height = dispHeight * 0.1,
onRelease = onBackBtnRelease
}
backBtn.anchorX = 0.5
backBtn.anchorY = 0.5
backBtn.x = display.contentWidth * 0.8
backBtn.y = dispHeight * 0.8
backBtn:toFront()
group:insert( backBtn )
group:toFront() --second try to put that button to front
end
Since the MapView is a native object I can't add it to any group and 'mapView:toBack()' doesn't work as well ('attempt to index upvalue 'myMap' (a nil value)'). In Corona SDK the button appears without the map, as expected. On device I get the map without my button, not as expected.
Is it any possible to put a native object to the back, or to force something to be in the front?
What you are trying to do with the map and the button is not currently possible with CoronaSDK.
Indeed the MapView is a native object. According to Corona's documentation, you cannot place other objects on top of a MapView.
The documentation says:
Map objects, like other native display objects, cannot be inserted into groups and are always displayed on top of regular Display Objects (groups, vector, images, and text).
See documentation here
Good luck coding.
First Check the Following :
If button doesn't appear on device most common problem will be , you might misspell the Image names(Check for Caps) or check the path of the file is proper or not.
To know in detail about the mapView : http://docs.coronalabs.com/api/library/native/newMapView.html
I know you might have visited already, its better you look again.
Thanks,

Lua - Corona SDK - Text not appearing inside a display Object

I am trying to move a display object that is hiding over the right side of the screen, into the scene. It works wonderfully with images (i.e. the background), but not with texts (the coords seem correct from debugging them with print(), but they never display, I already tried the obj:toFront).
I thought that they may work inside display objects, so I put everything in a display Object: Nothing. Just the text? Neither. Anyone knows why/how to override this?
function tscreen:init()
local textGroup = display.newGroup()
local menuBackground = self:getBtn("src/bgMenu.png")
menuBackground.isVisible = false
menuBackground.anchorX = 0.5
menuBackground.anchorY = 0.5
self.menuBackground = menuBackground
local optionsText = {
parent = textGroup,
text = "Hello World",
x = centerX,
y = centerY,
width = 128,
font = native.systemFontBold,
fontSize = 14,
align = "center"
}
local workText = display.newText( optionsText )
workText:setFillColor( 1, 0, 0 )
setPos(textGroup, W, 0)
--setPos() is a custom function that assigns x and y coords
textGroup.isVisible = false
self.textGroup = textGroup
end
function tscreen:show()
local menuBackground = self.menuBackground
local textGroup = self.textGroup
local inTime = 1200
setPos(menuBackground, 2*W + centerX, centerY)
menuBackground.isVisible = true
setPos(textGroup, W, 0)
textGroup.isVisible = true
self:cancelTween(menuBackground)
self:cancelTween(textGroup)
menuBackground.tween = transition.to(menuBackground, {time = inTime, transition = easing.outExpo, x = centerX,
onComplete = function()
tscreen:cancelTween(menuBackground)
end
})
textGroup.tween = transition.to(textGroup, {time = inTime, transition = easing.outExpo, x = 0,
onComplete = function()
tscreen:cancelTween(textGroup)
print(getPos(textGroup), textGroup.width, textGroup.height)
end
})
end
I have the starters edition of Corona, so I don't have the recently implemented Composer API.
Maybe this isn't the most appropriate site to post this query since there already is a Corona SDK forum, but I'm trying anyway.
I'm not seeing anything wrong, but a group should not be necessary. Verify that the text can be seen ever: in the init(), do
local optionsText = {
text = "Hello World",
x = 0,
y = 100,
}
local workText = display.newText( optionsText )
workText:setFillColor( 1, 1, 1 )
If you can't see the text then something else is going on, maybe your init() is not being called or such. Once you see it, change the parameters to what you want (fill color etc), and test. If still works, add a transition, right after, in the init():
local easeXto100 = {
time = 2000,
transition = easing.outExpo,
x = 100,
onComplete = function() print('did you see text move from x=0 to 100?') end
}
transition.to(workText, easeXto100)
If you see it move, then move the relevant parts of code to your show(), if now it disappears this will give you clue.

Character Selection in Corona SDK

I make an endless run game using Corona SDK and I need to make a character selection between 2 characters (boy/girl). I don't have any idea how I should start.
I tried to make 2 portraits of the character on the menu screen, but I don't know what to do on Event Touch on them. I tried to save them in a variable but I don't know how to load them in the game.lua. There I have:
local spriteSheet = sprite.newSpriteSheet("monsterSpriteSheet.png", 100, 100)
local monsterSet = sprite.newSpriteSet(spriteSheet, 1, 7)
sprite.add(monsterSet, "running", 1, 6, 600, 0)
sprite.add(monsterSet, "jumping", 7, 7, 1, 1)
local monster = sprite.newSprite(monsterSet)
monster:prepare("running")
monster:play()
monster.x = 60
monster.y = 200
monster.gravity = -6
monster.accel = 0
monster.isAlive = true
I've got a main.lua a menu.lua and a game.lua. I use director class for transition. Any ideas on how I can do this?
You can pass parameters through storyboard.gotoScene
local options = {
effect = "crossFade",
time = 500,
params = {
character = myCharacter,
}
}
storyboard.gotoScene( "game", options )
and in the game.lua
function scene:createScene( event )
local params = event.params
local character = params.character
end
You could also create a data file and point to that file.
For example:
data.lua
local data = {}
return data
Then in your selection scene require data.lua and save your chosen character to it.
data.chosenCharacter = chosenCharater
Then in your game scene require data.lua again and point your character to what is saved in data.
local character = data.chosenCharacter

Creating an image from a sprite sheet using the original images file name

Creating sprite sheets aka texture atlases rather than using many hundres of individual images is recommended everywhere. I have hundreds of images for a word learning game; but there are hundreds of words, no animation sequences. So having generated the data file and sprite sheet, i am looking for an example of how to create an image when needed from the original image file name (as stored in the sprite sheet data (lua code) file (both created with texture packer).
This much seems right:
local sprite = require("sprite")
local CN_70_tiles_corona = require("CN_70_tiles_corona")
local spriteDataCN = CN_70_tiles_corona.getSpriteSheetData()
local spriteSheet = sprite.newSpriteSheetFromData( "CN_70_tiles_corona.png", spriteDataCN )
before creating the sprite sheet, would create my image with something like this:
t1 = display.newImage(cnTiles[tileNO])
where cnTiles[1], for examples, is a value placed in an array from a sqlite table such as "sit_word100.png".
there is now an entry in my generate lua file below the 'getSpritSheetData' function something like this:
{
name = "sit_word100.png",
spriteColorRect = { x = 0, y = 0, width = 69, height = 69 },
textureRect = { x = 2, y = 2, width = 69, height = 69 },
spriteSourceSize = { width = 69, height = 69 },
spriteTrimmed = false,
textureRotated = false
},
i can see that ALL my image file names are now stored in the data to provide a way to refer to my image within the sprite sheet, but since i do NOT want to use "sprite sets", i can't find an example of just getting the one image when in eed it.
I want something that allows me to refer to my now spritesheet-ified image using the original image name. is this possible? e.g.
t1 = display.newImage(CN_70_tiles_corona.getSpriteSheetData(name = "sit_word100.png")
The easy way is to create sheets with TexturePacker and use SpriteGrabber to take the sprites your need.
It's an awesome add-on to Corona-SDK which can be found here:
http://developer.anscamobile.com/code/spritegrabber-spritesheets-two-lines

In Corona SDK how to add label(text) to images?

In corona SDK how to add label(text) to images
I have created my image as follow
local item = display.newImageRect('images/foo.png',70,70);
item.x = 50;
item.y = 50;
How to add a text to the image?
Many Thanks
To add text to the image, so it appears to "stick" to the image, you could create the object (as you have) and also the text object directly above it. Then, create a group and stick them both in it.
Here is an example of how that could be done:
local myGroup = display.newGroup()
local item = display.newImageRect('images/foo.png',70,70)
item.x = 50
item.y = 50
local text = display.newText( "My Text", 0, 0, "Helvetica", 18 )
text:setTextColor( 0, 0, 0, 255 )
-- insert items into group, in the order you want them displayed:
myGroup:insert( item )
myGroup:insert( text )
Then, to move it around, just modify the x/y of 'myGroup'.
Hope that helps!

Resources