简体   繁体   中英

attempt to call method 'applyForce'

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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM