Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Hey in my project's game.lua file touch event is only working on dart display object once Second time it won't work
Started by jangidr413 Oct 03 2018 11:40 PM

1 reply to this topic
#eventlistener
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

jangidr413

[GLOBAL: userInfoPane.html]
jangidr413
  • Observer

  • 1 posts
  • Corona SDK

local composer = require( "composer" )

local scene = composer.newScene()

local backGroup
local mainGroup
local uiGroup
local balloon
local str="red"
local line
local dragy,dart1y,prvdart1y,dart1x
local Angle=0,speed
local speedRect1,speedRect2,speedRect2,speedRect4,speedRect5
local dart


local function produceBalloon()


local rn=math.random(0,8)
if(rn == 1)
then
str="yellow"
elseif(rn == 2)
then
str="pink"
elseif(rn == 3)
then
str="darkgreen"
elseif(rn == 4)
then
str="green"
elseif(rn == 5)
then
str="purple"
elseif(rn == 6)
then
str="red"
elseif(rn == 7)
then str="white"
end

balloon = display.newImageRect( mainGroup,str .. ".png", 50,64 )
balloon.x=math.random(display.actualContentWidth/2,display.actualContentWidth-30)
balloon.y=math.random(display.actualContentHeight/2,display.actualContentHeight-30)
physics.addBody( balloon, "dynamic", { bounce=0.3,radius=30 } )
balloon.gravityScale=0
balloon:setLinearVelocity(0,-20)
end




function scene:create( event )

local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen

backGroup = display.newGroup() -- Display group for the background image
sceneGroup:insert( backGroup ) -- Insert into the scene's view group

mainGroup = display.newGroup() -- Display group for the ship, asteroids, lasers, etc.
sceneGroup:insert( mainGroup ) -- Insert into the scene's view group

uiGroup = display.newGroup() -- Display group for UI objects like the score
sceneGroup:insert( uiGroup )

speedGroup = display.newGroup() -- Display group for UI objects like the score
sceneGroup:insert( uiGroup )


local BackGround = display.newRect(backGroup, display.actualContentWidth/2,display.actualContentHeight/2 , display.actualContentWidth, display.actualContentHeight )
BackGround:setFillColor( .56,.44,.39)

local platform = display.newImageRect( mainGroup,"platform.png", 80, 20 )
platform.x = display.contentCenterX/3
platform.y = display.contentCenterY+50



dart = display.newImageRect( mainGroup,"dartgame.png", 30,30 )
dart.x = platform.x-25
dart.y = platform.y-78

local char1 = display.newImageRect( mainGroup,"th1.png", 57,99 )
char1.x = platform.x
char1.y = platform.y-59
local char2,char3




local function clearMain()

mainGroup:removeSelf()
end


function dart:touch( event )
local dart1 = event.target
local phase = event.phase
local yNow = event.target.y

if ( phase == "began" ) then
display.currentStage:setFocus( dart1 )
display.getCurrentStage():setFocus( self )
self.isFocus = true
line = display.newImageRect( mainGroup,"line.png", 48,20 )
line.x = dart1.x-25
line.y = dart1.y+23
local speedRect1 = display.newRect(speedGroup, 20, 70, 8, 10 )
speedRect1:setFillColor(1,1,1)
speed=150

-- Store initial position
dart1.y0 = event.y

elseif self.isFocus then

if ( phase == "moved" ) then
dart1y = event.y - dart1.y0

if dart1y>0 then
if prvdart1y then
if dart1y>prvdart1y then Angle=Angle+3 dart1:rotate(-3)
elseif dart1y<prvdart1y then Angle=Angle-3 dart1:rotate(3)
end
end
else
if prvdart1y then
if dart1y>prvdart1y then Angle=Angle+3 dart1:rotate(-3)
elseif dart1y<prvdart1y then Angle=Angle-3 dart1:rotate(3)
end
end
end


dart1x=event.xStart-event.x
if(dart1x<=10)
then
speedGroup:removeSelf()
speedGroup = display.newGroup() -- Display group for UI objects like the score
sceneGroup:insert( uiGroup )

speedRect1 = display.newRect(speedGroup, 20, 70, 8, 10 )
speedRect1:setFillColor(1,1,1)
speed=150
end
if(dart1x>=10 and dart1x<=20)
then
speedGroup:removeSelf()
speedGroup = display.newGroup() -- Display group for UI objects like the score
sceneGroup:insert( uiGroup )

speedRect2 = display.newRect(speedGroup, 20, 70, 8, 15 )
speedRect2:setFillColor(1,1,1)
speed=230
end
if(dart1x>=20 and dart1x<=30)
then
speedGroup:removeSelf()
speedGroup = display.newGroup() -- Display group for UI objects like the score
sceneGroup:insert( uiGroup )

speedRect3 = display.newRect(speedGroup, 20, 70, 8, 20 )
speedRect3:setFillColor(1,1,1)
speed=300

end
if(dart1x>=30 and dart1x<=40)
then
speedGroup:removeSelf()
speedGroup = display.newGroup() -- Display group for UI objects like the score
sceneGroup:insert( uiGroup )
speedRect4 = display.newRect(speedGroup, 20, 70, 8, 25 )
speedRect4:setFillColor(1,1,1)
speed=400
end
if(dart1x>=40)
then
speedGroup:removeSelf()
speedGroup = display.newGroup() -- Display group for UI objects like the score
sceneGroup:insert( uiGroup )

speedRect5 = display.newRect(speedGroup, 20, 70, 8, 25 )
speedRect5:setFillColor(1,1,1)
speed=500
end

prvdart1y=dart1y

elseif ( phase == "ended" ) then
transition.to( dart1, { time=100,onComplete = function() display.remove( char1 ) char2 = display.newImageRect( mainGroup,"th2.png", 57,99 )
char2.x = platform.x
char2.y = platform.y-59 end } )
transition.to( dart1, { delay=100,time=100,onComplete = function() display.remove( char2 ) char3 = display.newImageRect( mainGroup,"th3.png", 57,99 )
char3.x = platform.x
char3.y = platform.y-59
end } )
transition.to( dart1, { delay=300,time=100,onComplete = function() display.remove( char3 ) char1 = display.newImageRect( mainGroup,"th1.png", 57,99 )
char1.x = platform.x
char1.y = platform.y-59
end } )

local function printDart()
local dart = display.newImageRect( mainGroup,"dartgame.png", 30,30 )
dart.x = platform.x-25
dart.y = platform.y-78
end

local function pushDart()
physics.addBody( dart1, "dynamic",{density=3})
dart1:setLinearVelocity( speed*math.cos((Angle+45)*0.0174533), -speed*math.sin((Angle+45)*0.0174533) )
end
pushDart()
display.getCurrentStage():setFocus( nil )
self.isFocus = nil
speedGroup:removeSelf()
printDart()
end
return true
end
end

local function onKeyEvent( event )
local phase = event.phase;
if (phase=="up") then
return true
elseif (phase=="down") then
-- do nothing
end
return true
end

local function gameLoop()
produceBalloon()
end


local physics = require( "physics" )
physics.start()
dart:addEventListener( "touch", dart )
Runtime:addEventListener( "key", onKeyEvent )
gameLoopTimer = timer.performWithDelay( 5000, gameLoop, 0 )

end

-- show()
function scene:show( 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
physics.start()
--Runtime:addEventListener( "collision", onCollision )
gameLoopTimer = timer.performWithDelay( 500, gameLoop, 0 )

end
end


-- hide()
function scene:hide( event )

local sceneGroup = self.view
local phase = event.phase

if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)

elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
speedGroup:removeSelf()
composer.removeScene( "game" )
Runtime:removeEventListener( "touch", dartTouchListener )
Runtime:removeEventListener( "key", onKeyEvent )


end
end


-- destroy()
function scene:destroy( event )

local sceneGroup = self.view

-- Code here runs prior to the removal of scene's view

end


-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------

return scene
  • jangidr413 likes this

[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,714 posts
  • Corona Staff

Hi @jangidr413 and welcome to our community forums. 

 

Just a couple of notes, you're probably not going to get a lot of people looking at this for a couple of reasons. 

 

First, you put your entire question in the subject/headline/title of the post. Clearly, we want that to be more than "Help me", but putting the entire question there.

 

Secondly, dumping unformatted code into a post makes it hard to read. We recommend you use the blue <> button in the row with Bold and Italics. Click that button and paste your code into the popup window. It will help people read your code better.

 

Also dumping in full scenes shouldn't be done unless asked for. Instead, narrow down where you think the problem is.

 

The people here want to be helpful, but we have a saying "Help us Help you".

 

Rob




[topic_controls]
[/topic_controls]