Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Bug in the Star Explorer Tutorial, or have I messed up?
Started by Capzor Feb 25 2019 08:01 AM

2 replies to this topic
star explorer touchoffsetx

Best Answer Capzor , 25 February 2019 - 10:07 AM

Edit #2:

OK this fixed it:

local function dragShip( event )
 
    local ship = event.target
    local phase = event.phase
 
    if ( "began" == phase ) then
        -- Set touch focus on the ship
        display.currentStage:setFocus( ship )
        ship.isFocus = true
        -- Store initial offset position
        ship.touchOffsetX = event.x - ship.x
 
    elseif ( ship.isFocus ) then
        if ( "moved" == phase ) then
            -- Move the ship to the new touch position
            ship.x = event.x - ship.touchOffsetX
 
    elseif ( "ended" == phase or "cancelled" == phase ) then
        -- Release touch focus on the ship
        display.currentStage:setFocus( nil )
        end
    end
    return true  -- Prevents touch propagation to underlying objects
end

Edit: i think i have some idea of maybe what is happening:

 

The error occurs when you start the touch as the scene is fading in, and continue moving the same touch after the scene has completed the transition.

 

as the scene is transitioning in, the "began" phase does not register - you cannot move the ship for some reason. I'm not sure why.

 

But that's the phase where touchOffsetX is defined. so it doesn't get defined.

So then you're able to move or end the touch once the scene has transitioned, without beginning the touch, so a touchOffsetX is not defined.. which it needs, so there's an error.

 

I also tested it with the source files from the tutorial, and the same thing happens.

 

 

 

Thanks Rob!

 

Yes that is exactly the correct line :)

 

The thing is, if I wait for the scene to "fade in" completely, there is no problem - it's only an error if I try to move the ship a fraction of a second after pressing the Play button, as the scene is still fading in.

 

I made some prints:

local function dragShip( event )
 
    local ship = event.target
    local phase = event.phase
 
    if ( "began" == phase ) then
        -- Set touch focus on the ship
        display.currentStage:setFocus( ship )
        -- Store initial offset position
        ship.touchOffsetX = event.x - ship.x
        print(ship.touchOffsetX)
        print(phase)
 
    elseif ( "moved" == phase ) then
        -- Move the ship to the new touch position
        ship.x = event.x - ship.touchOffsetX
        print(ship.x)
        print(phase)
 
    elseif ( "ended" == phase or "cancelled" == phase ) then
        -- Release touch focus on the ship
        display.currentStage:setFocus( nil )
        print(phase)
    end
 
    return true  -- Prevents touch propagation to underlying objects
end

and the began, moved, and ended phases seem to happen as expected, as long as the scene has completely faded in.

19:35:45.431  began
19:35:45.805  279.46664428711
19:35:45.805  moved
19:35:46.025  ended
19:35:48.035  3.0517578125e-005
19:35:48.035  began
19:35:48.563  281.59997558594
19:35:48.563  moved
19:35:48.905  ended

But the same error happens if I try to move the ship while it's still loading in the scene. Perhaps it has something to do with this?:

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
		-- Code here runs when the scene is entirely on screen
        physics.start()
        Runtime:addEventListener( "collision", onCollision )
        gameLoopTimer = timer.performWithDelay( 500, gameLoop, 0 )
        -- Start the music!
        audio.play( musicTrack, { channel=1, loops=-1 } )
	end
end

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Capzor

[GLOBAL: userInfoPane.html]
Capzor
  • Observer

  • 2 posts
  • Corona SDK

What I was trying to do:
When running the completed version of the Star Fighter tutorial in the Corona Simulator, I was trying to move (drag) the ship on the screen immediately after pressing on the "Play" button, before the scene has totally faded in.
 
What I expected the result to be:
The ship to move
 
What happened instead:
Corona spits out the following error:
 
ERROR: Runtime error
game.lua:159: attempt to perform arithmetic on field 'touchOffsetX' (a nil value)
stack traceback:
game.lua:159: in function '?'
?: in function <?:190>
  • What version of Corona SDK you I am running and which version of Windows
  • Corona Simulator 2018.3326 (2018.6.25)
  • Windows 10 Version 1803

 

I guess it has something to do with this:

local function dragShip( event )
 
    local ship = event.target
    local phase = event.phase
 
    if ( "began" == phase ) then
        -- Set touch focus on the ship
        display.currentStage:setFocus( ship )
        -- Store initial offset position
        ship.touchOffsetX = event.x - ship.x
 
    elseif ( "moved" == phase ) then
        -- Move the ship to the new touch position
        ship.x = event.x - ship.touchOffsetX
 
    elseif ( "ended" == phase or "cancelled" == phase ) then
        -- Release touch focus on the ship
        display.currentStage:setFocus( nil )
    end
 
    return true  -- Prevents touch propagation to underlying objects
end

Here is the whole code for the game.lua:

Spoiler

 

P.S. I tried to use the lua tags for pasting in lua code, but I couldn't get it to work correctly - it didn't show the indentations or line numbers when I preview the post.

 

Thanks for any help!



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,529 posts
  • Enterprise

What is game.lua line 159?  I suspect it's

ship.x = event.x - ship.touchOffsetX

which tells me that ship.touchOffsetX didn't get initialized properly. The question is why? 

 

Perhaps adding some prints to make sure you're getting a "began" phase might be useful.

 

Rob


  • Capzor likes this

[TOPIC: post.html]
#3

Capzor

[GLOBAL: userInfoPane.html]
Capzor
  • Observer

  • 2 posts
  • Corona SDK

  Best Answer

Edit #2:

OK this fixed it:

local function dragShip( event )
 
    local ship = event.target
    local phase = event.phase
 
    if ( "began" == phase ) then
        -- Set touch focus on the ship
        display.currentStage:setFocus( ship )
        ship.isFocus = true
        -- Store initial offset position
        ship.touchOffsetX = event.x - ship.x
 
    elseif ( ship.isFocus ) then
        if ( "moved" == phase ) then
            -- Move the ship to the new touch position
            ship.x = event.x - ship.touchOffsetX
 
    elseif ( "ended" == phase or "cancelled" == phase ) then
        -- Release touch focus on the ship
        display.currentStage:setFocus( nil )
        end
    end
    return true  -- Prevents touch propagation to underlying objects
end

Edit: i think i have some idea of maybe what is happening:

 

The error occurs when you start the touch as the scene is fading in, and continue moving the same touch after the scene has completed the transition.

 

as the scene is transitioning in, the "began" phase does not register - you cannot move the ship for some reason. I'm not sure why.

 

But that's the phase where touchOffsetX is defined. so it doesn't get defined.

So then you're able to move or end the touch once the scene has transitioned, without beginning the touch, so a touchOffsetX is not defined.. which it needs, so there's an error.

 

I also tested it with the source files from the tutorial, and the same thing happens.

 

 

 

Thanks Rob!

 

Yes that is exactly the correct line :)

 

The thing is, if I wait for the scene to "fade in" completely, there is no problem - it's only an error if I try to move the ship a fraction of a second after pressing the Play button, as the scene is still fading in.

 

I made some prints:

local function dragShip( event )
 
    local ship = event.target
    local phase = event.phase
 
    if ( "began" == phase ) then
        -- Set touch focus on the ship
        display.currentStage:setFocus( ship )
        -- Store initial offset position
        ship.touchOffsetX = event.x - ship.x
        print(ship.touchOffsetX)
        print(phase)
 
    elseif ( "moved" == phase ) then
        -- Move the ship to the new touch position
        ship.x = event.x - ship.touchOffsetX
        print(ship.x)
        print(phase)
 
    elseif ( "ended" == phase or "cancelled" == phase ) then
        -- Release touch focus on the ship
        display.currentStage:setFocus( nil )
        print(phase)
    end
 
    return true  -- Prevents touch propagation to underlying objects
end

and the began, moved, and ended phases seem to happen as expected, as long as the scene has completely faded in.

19:35:45.431  began
19:35:45.805  279.46664428711
19:35:45.805  moved
19:35:46.025  ended
19:35:48.035  3.0517578125e-005
19:35:48.035  began
19:35:48.563  281.59997558594
19:35:48.563  moved
19:35:48.905  ended

But the same error happens if I try to move the ship while it's still loading in the scene. Perhaps it has something to do with this?:

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
		-- Code here runs when the scene is entirely on screen
        physics.start()
        Runtime:addEventListener( "collision", onCollision )
        gameLoopTimer = timer.performWithDelay( 500, gameLoop, 0 )
        -- Start the music!
        audio.play( musicTrack, { channel=1, loops=-1 } )
	end
end



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: star explorer, touchoffsetx