In my scenario, I am trying to create a validation logic within render return. I have asycstorage value I am getting value and validate by using below code
render() {
const value = AsyncStorage.getItem(‘username’);
return (
{value !== null &&
(<View
……………..
</View> )}
)}
Above code not validating also based on validation result it is not rendering. If value not equal to null mean, I need to render view. How to achieve this?
As said, AsyncStorage returns a promise. A way to do what you want is to use state to know when the promise has been completed. However you have to call your AsyncStorage function before render.
didComponentMount() {
AsyncStorage.getItem(‘username’).then((res)=>{this.setState({gotUsername: true}); value = res;});
}
render(){
if(!this.state.gotUsername) {
return value !== null &&
(<View
</View> )
}
}
I have a main scene in the Corona SDK (main.lua) On this scene there is a button, when you click on which there should be a transition to another scene (home.lua). How do I do this? Preferably using composer
Modified version of example from documentation
local composer = require( "composer" )
local scene = composer.newScene()
...
local object = display.newImage( "ball.png" )
local function onObjectTouch( event )
if ( event.phase == "began" ) then
elseif ( event.phase == "ended" ) then
composer.gotoScene( "home" )
end
return true
end
object:addEventListener( "touch", onObjectTouch )
I'm making a side scroller and when i start my game i can touch the screen to keep my penguin in the air, but when i fail and collide with an ice block and press play after going to restart.lua i get the error attempt to call method 'applyForce'
heres my code
local function activatePengs(self,event)
self:applyForce(0, -45, self.x, self.y)
print("run")
end
local function touchScreen(event)
print("touch")
if event.phase == "began" then
peng.enterFrame = activatePengs
Runtime:addEventListener("enterFrame", peng)
end
if event.phase == "ended" then
Runtime:removeEventListener("enterFrame", peng)
end
end
local function onCollision(event)
if event.phase == "began" then
print "collide"
composer.gotoScene( "restart",{ time=800, effect="crossFade" } )
end
end
-- now comes four required functions for Composer:
function scene:create( event )
local sceneGroup = self.view
bg1 = display.newImageRect(sceneGroup, "bg.png", 800, 1000)
bg2 = display.newImage(sceneGroup, "ice2.png",140,210)
bg3 = display.newImage(sceneGroup, "ice2.png",540,210)
bg4 = display.newImage(sceneGroup, "ice2.png",940,210)
bg5 = display.newImage(sceneGroup, "ice2.png",1340,210)
bg6 = display.newImage(sceneGroup, "ice2.png",1740,210)
bg7 = display.newImage(sceneGroup, "ice1.png",140,420)
bg8 = display.newImage(sceneGroup, "ice1.png",540,420)
bg9 = display.newImage(sceneGroup, "ice1.png",940,420)
bg10 = display.newImage(sceneGroup, "ice1.png",1340,420)
bg11 = display.newImage(sceneGroup, "ice1.png",1740,420)
peng = display.newImage(sceneGroup, "peng.png", 80, 201)
physics.addBody(peng, "dynamic", {density=.18, bounce=0.1, friction=.5, radius=55})
...
end
Probably the error occurs because penguin is not a physics object when the function is called. I think remove enterFrame listener before go to next scene should solve your problem.
I added some improvements to your code (not tested) :
local applyForceToPenguin = false
local function enterFrame( self, event )
if applyForceToPenguin then
self:applyForce( 0, -45, self.x, self.y )
print("run")
end
end
local function touchScreen(event)
print("touch")
if event.phase == "began" then applyForceToPenguin = true end
if event.phase == "ended" then applyForceToPenguin = false end
end
local function onCollision(event)
if event.phase == "began" then
print "collide"
applyForceToPenguin = false
composer.gotoScene( "restart",{ time=800, effect="crossFade" } )
end
end
-- now comes four required functions for Composer:
function scene:create( event )
local sceneGroup = self.view
bg1 = display.newImageRect( sceneGroup, "bg.png", 800, 1000 )
bg2 = display.newImage( sceneGroup, "ice2.png", 140, 210 )
bg3 = display.newImage( sceneGroup, "ice2.png", 540, 210 )
bg4 = display.newImage( sceneGroup, "ice2.png", 940, 210 )
bg5 = display.newImage( sceneGroup, "ice2.png", 1340, 210 )
bg6 = display.newImage( sceneGroup, "ice2.png", 1740, 210 )
bg7 = display.newImage( sceneGroup, "ice1.png", 140, 420 )
bg8 = display.newImage( sceneGroup, "ice1.png", 540, 420 )
bg9 = display.newImage( sceneGroup, "ice1.png", 940, 420 )
bg10 = display.newImage( sceneGroup, "ice1.png", 1340, 420 )
bg11 = display.newImage( sceneGroup, "ice1.png", 1740, 420 )
peng = display.newImage( sceneGroup, "peng.png", 80, 201)
physics.addBody( peng, "dynamic", { density=.18, bounce=0.1, friction=.5, radius=55 } )
Runtime:addEventListener( "enterFrame", enterFrame )
...
end
function scene:destroy()
Runtime:removeEventListener( "enterFrame", enterFrame )
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif phase == "did" then
-- Code here runs when the scene is entirely on screen
applyForceToPenguin = false
end
end
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
Have a nice day:)
In my game I have a scrollview widget declared inside a function, and I want to remove that scrollview using another function, something like this:
local function createScrollView(event)
if(event.phase=="ended")then
local function scrollListener( event )
local phase = event.phase
local direction = event.direction
if "began" == phase then
--print( "Began" )
elseif "moved" == phase then
print( "Moved" )
elseif "ended" == phase then
--print( "Ended" )
end
-- If the scrollView has reached it's scroll limit
if event.limitReached then
if "up" == direction then
print( "Reached Top Limit" )
elseif "down" == direction then
print( "Reached Bottom Limit" )
elseif "left" == direction then
print( "Reached Left Limit" )
elseif "right" == direction then
print( "Reached Right Limit" )
end
end
return true
end
-- Create a ScrollView
local scrollView = widget.newScrollView
{
left = display.contentWidth/2-130,
top = display.contentHeight/2-165,
width = 440,
height = 300,
bottomPadding = 10,
id = "onBottom",
backgroundColor = { 0, 0, 0, 0 },
horizontalScrollDisabled = true,
verticalScrollDisabled = false,
listener = scrollListener,
}
end
end
local function removeScrollView(event)
if(event.phase==ended)then
if scrollView then
scrollView:removeSelf()
scrollView = nil
end
end
end
It doesn't return any error, but I can see that the scrollView still exists because the Corona Terminal keeps printing "moved".
Do you know how could I remove it?
local scrollView ------------EDITED
local function createScrollView(event)
if(event.phase=="ended")then
local function scrollListener( event )
local phase = event.phase
local direction = event.direction
if "began" == phase then
--print( "Began" )
elseif "moved" == phase then
print( "Moved" )
elseif "ended" == phase then
--print( "Ended" )
end
-- If the scrollView has reached it's scroll limit
if event.limitReached then
if "up" == direction then
print( "Reached Top Limit" )
elseif "down" == direction then
print( "Reached Bottom Limit" )
elseif "left" == direction then
print( "Reached Left Limit" )
elseif "right" == direction then
print( "Reached Right Limit" )
end
end
return true
end
-- Create a ScrollView
---EDITED---------------------
scrollView = widget.newScrollView
{
left = display.contentWidth/2-130,
top = display.contentHeight/2-165,
width = 440,
height = 300,
bottomPadding = 10,
id = "onBottom",
backgroundColor = { 0, 0, 0, 0 },
horizontalScrollDisabled = true,
verticalScrollDisabled = false,
listener = scrollListener,
}
end
end
local function removeScrollView(event)
if(event.phase==ended)then
if scrollView then
scrollView:removeSelf()
scrollView = nil
end
end
end
So that you can remove the scroll view in the removeScrollView function.
I'm having a problem when I try to restart my game. I pause the physics, then when the user clicks the button, the physics are supposed to restart. When I put in physics.start(), though the game crashes. So, I was wondering if it's possible to pause one physical object and then change it's position on the screen. I'm kind of a newb so any answer will be helpful.
local physics = require( "physics")
physics.start( )
local crate1 = display.newRect( display.contentWidth/2,display.contentHeight/2, 40,40)
physics.addBody( crate1, { density=4.0, friction=0.3, bounce=.4} )
crate1.bodyType = "dynamic"
crate1.isBodyActive = true
crate1:setFillColor( 1,0,.3)
sky = display.newRect( display.contentWidth/2, .5, display.contentWidth, 0.000000000000000000001)
physics.addBody( sky, {density=4.0, friction=0.3, bounce=1.5} )
sky.bodyType = "static"
ground = display.newRect( display.contentWidth/2, display.contentHeight, display.contentWidth, .00000000001 )
physics.addBody( ground, {density=4.0, friction=0.3, bounce=1.5 } )
ground.bodyType = "static"
rightWall = display.newRect( display.contentWidth, display.contentHeight/2, 1, display.contentHeight )
physics.addBody( rightWall, {density=4.0, friction=0.3, bounce=1.5} )
rightWall.bodyType = "static"
leftWall = display.newRect(1, display.contentHeight/2, .0000000001, display.contentHeight )
physics.addBody( leftWall, {density=4.0, friction=0.3, bounce=1.5} )
leftWall.bodyType = "static"
physics.setGravity( 0, 3 )
local function handleCollisionOnDelay( event )
local obj = event.source.object
obj.isBodyActive = false
end
local function onLocalCollision( self, event )
if ( event.phase == "began" ) then
local dr = timer.performWithDelay( 10, handleCollisionOnDelay )
dr.object = self
crate1.x = display.contentWidth/2
crate1.y = display.contentHeight/2
end
end
crate1.collision = onLocalCollision
crate1:addEventListener( "collision", crate1 )
use isBodyActive property
http://docs.coronalabs.com/api/type/Body/isBodyActive.html
good luck