MapView to background - Corona SDK - lua

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,

Related

CoronaSDK Touch Events

Currently creating a game with Corona SDK is it possible to have an image and when it is clicked it displays 3 images and once one them 3 images are clicked the score increases by 1. Also Im only a beginner at coding , this is a new language to me. Thanks.
local CButton = display.newImage("+5.jpg" , 100 , 600)
CButton.alpha = 0.5
CButton.name = "CButton"
local CButtonLabel = display.newText( { text = "", x = 0, y = 0, fontSize = 28 } )
CButtonLabel:setTextColor( 0 ) ; CButtonLabel.x = 100 ; CButtonLabel.y = 45
local function touchCListener( event )
local object = event.target
print( event.target.name.." TOUCH on the '"..event.phase.."' Phase!" )
local ChordCOne = display.newImage("+5.jpg", 900,300)
local ChordCTwo = display.newImage("+5.jpg", 1000,300)
local ChordCThree = display.newImage("+5.jpg", 1100,300)
end
--add "touch" listener -- LABEL IS FOR TESTING!
CButton:addEventListener( "touch", touchCListener)
ChordCOne:addEventListener( "touch", updateScore)
CButtonLabel.text = "touch"
Yes, new DisplayObjects can be created in a listener function and listeners can be added to those objects as well.
In your code, you have not added the DisplayObjects created in your listener to any GroupObject (such as scene.view), which will give unexpected results.
Since the variables pointing to the newly created DisplayObjects (ChordCOne, etc.) are local to the function where they are instantiated, you cannot call addEventListener() on them outside the function. You should add the listener when they are created.
Also, the updateScore() listener function isn't defined anywhere. Make sure updateScore is not nil when and wherever you give it as an argument to addEventListener().

ScrollView or NewText is not infinite

I am making a changelog for my app and I inserted a bunch of text into display.newText. Is there a limit to the amount of characters display.newText can take or is it a problem in ScrollView? My code:
local widget = require( "widget" )
scrollView = widget.newScrollView
{
hideBackground = true,
hideScrollBar = true,
left = 0,
top = 170,
width = display.contentWidth,
height = contH - 300,
topPadding = 150,
bottomPadding = 20,
horizontalScrollDisabled = true,
verticalScrollDisabled = false,
}
ChangeLogText = [[ ...a bunch of text here... ]]
ChangeLog = display.newText(ChangeLogText, contW / 2, 4730, contW - 100, 0, "Fonts/visitor1.ttf", 40)
scrollView:insert(ChangeLog)
I can see ~164 lines of text, but it just stops there... Is there a way to make it infinite?
I had the same problem just recently - too bad that such limitations are detailed nowhere in the API.
I started with this github repository, witch contains the full widget library of Corona SDK (it's no longer developed AFAIK), but managed to find nothing.
The workaround I eventually settled with, is to start with only the first part of the text inserted into the text display object, and then use scrollView scrollListener to reload text whenever top/bottom limit is reached.
Part of scrollListener (event.direction == "up" is basically the mirror image of this):
elseif ( event.direction == "down" ) then
--check if limit is reached or close to it, y is from scrollView:getContentPosition(), -800 is arbitrary
if ( event.limitReached ) or y > - 800 then print( "Reached top limit" )
--firstS stores number of first visible sign of a string
if chat.firstS > 1 then
--copy string
local shownText = stringText
--update display object
chat.text = shownText
local firstS, lastS
--do while display object is too high - I found that scrollView stops scrolling ~25000 height
while (chat.height) > 24000 do
--basically cut the text in half
firstS = chat.firstS - #shownText/4
if firstS < 1 then firstS = 1 end
lastS = firstS + #shownText/2
shownText = string.sub( stringText, firstS, lastS)
chat.text = shownText
end
chat.firstS = firstS
chat.lastS = lastS
--scroll to halfway position in no time for seamless transition
mainView:scrollToPosition({y = -(chat.height)/2, time = 0})
end
end
end
This is an early version and needs some refinement, but I hope it helps. If you find anything better, let me know. Also, I display text from bottom, so you might need to adjust it accordingly.

Lua function only works on first click

I'm new to Lua, and I'm trying to do a simple program to change the location of an object randomly when it's clicked. The problem is, this program only works once, that is to say, upon loading the program and tapping the circle, it moves, but will not move again on subsequent taps. Any ideas?
local _W = display.contentWidth
local _H = display.contentHeight
math.randomseed(os.time())
math.random()
myCircle = display.newCircle(_W * 0.25, _H * 0.25, 50)
local function moveCircle(event)
h_random = math.random()
w_random = math.random()
display.remove(myCircle)
myCircle = display.newCircle(_W * w_random, _H * h_random, 50)
return true
end
myCircle:addEventListener("tap",moveCircle)
Many thanks in advance
I don't have any idea of what library you are using :) But try adding
myCircle:addEventListener("tap",moveCircle)
before return true in the moveCircle function.
From what I can understand from a quick inspection of the code snippet, the proposed change will bind the tap event to the newly created circle.

Image displays in top left corner in Corona Simulator

I just started playing with lua / Corona, and have been using the book "Create Mobile Games with Corona." (Silvia Domenech) The first project in the book is to make a simple planet defense game. For this, I am using a multi-screen application (or "scene" as it's called in OS X). The very first step is displaying an image using groups in Corona.
The book has me enter the following code in the main scene group:
local image = display.newImage( "images/iphone_767.png")
group:insert( image )
After adding it to (what I think is) the correct group, the code looks like this:
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
-----------------------------------------------------------------------------
local image = display.newImage( "images/iphone_767.png")
group:insert( image )
-- CREATE display objects and add them to 'group' here.
-- Example use-case: Restore 'group' from previously saved state.
-----------------------------------------------------------------------------
end
For the image, I first tried an image that was 320 x 480. When it rendered on the simulator, it is situated way off to the top left of the simulator. Here is a screenshot of how it renders: http://imgur.com/Kpm0XTd
The config file is set for 320 x 480 px. I'm really at a loss as to what could be causing this since I haven't modified anything outside of what I described. Any ideas?
Set the x and y values of the token to screen center (assuming you want to center it):
image.x = display.contentWidth/2
image.y = display.contentHeight/2
Thanks
Anand
I personally like setting a variable to display.contentWidth and display.contentWidth.
_W = display.contentWidth
_H = display.contentHeight
image.x = _W/2
image.y = _H/2
You could also format it like so:
image.x = _W*.5
image.y = _W*.5
Corona SDK has recently started using a new graphics engine that affects how things are positioned. The display.newImage() used to draw the top left corner at 0, 0, but now it draws the center at 0, 0. Your best bet is to always explicitly set the .x and .y of the image where you want it.
Seems there is simplified way exists now:
image.x = display.contentCenterX
image.y = display.contentCenterY
Or single line:
local image = display.newImage('images/iphone_767.png', display.contentCenterX, display.contentCenterY)
But now it is seems more conventional pattern is to create centeredGroup first:
-- Create centered group
local centeredGroup = display.newGroup()
centeredGroup.x = display.contentCenterX
centeredGroup.y = display.contentCenterY
-- Add background
local image = display.newImage(centeredGroup, 'images/iphone_767.png', display.contentCenterX, display.contentCenterY)

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