Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

attempt to call method 'applyLinearImpulse' (a nil value)
Started by botafogo0382 Dec 02 2019 07:37 AM

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

botafogo0382

[GLOBAL: userInfoPane.html]
botafogo0382
  • Observer

  • 6 posts
  • Corona SDK

I tried to make my character jump with applyLinearImpulse in two scene. On first, my character is jumping very high and leaves from the game screen. On Second this error happened: "attempt to call method 'applyLinearImpulse' (a nil value)"

 

I'm going to post de code from the two archives.

-- Bibliotecas

local composer = require( "composer" )
local scene = composer.newScene()
local physics = require "physics"
local widget = require "widget"

-- Algumas Configurações
local w = display.contentWidth
local h = display.contentHeight

local sheetData = { width=83, height=121, numFrames=24}
local sheetDataZombie= { width=83, height=100, numFrames=13}

local sheetZombie = graphics.newImageSheet("zombie.png", sheetDataZombie)
local sheet = graphics.newImageSheet("percy.png", sheetData)

local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

function goToNextLevel()
    
    composer.gotoScene("lastLevel", "fade", 700)
    composer.removeScene("level1")
end
local function invertSequence()
    if toLeft == true then
        toLeft = false
        toRight = true
    elseif toRight == true then
        toLeft = true
        toRight = false
    end
end

-- Inicio do jogo
function scene:create( event )
	local sceneGroup = self.view
    physics.start()
    physics.setGravity(0, 9)
    local background = display.newImageRect( "game_background.jpg", screenW, screenH )
	background.anchorX = 0 
    background.anchorY = 0
    background.x = 0 + display.screenOriginX 
	background.y = 0 + display.screenOriginY

    borda1 = display.newRect(-700, 0, 20, 5000)
    borda1.alpha = 0
    physics.addBody( borda1, "static" )

    borda1 = display.newRect(1450, 0, 20, 5000)
    borda1.alpha = 0
    physics.addBody( borda1, "static" )

    local parede1 = display.newImageRect("grass.png", 5000, 100)
    parede1.x = 60
    parede1.y = 1024
    physics.addBody( parede1, "static" )

    local parede2 = display.newImageRect("grass.png", 100, 60)
    parede2.x = 70
    parede2.y = 760
    physics.addBody( parede2, "static" )

    local parede6 = display.newImageRect("grass.png", 100, 80)
    parede6.x = 870
    parede6.y = 540
    physics.addBody( parede6, "static" )

    local porta = display.newImageRect("porta.png", 120, 150)
    porta.x = 450
    porta.y = 900
    porta.myName = "porta"
    physics.addBody( porta, "static", {radius = 10, isSensor = true})

    
    local parede7 = display.newImageRect("grass.png", 100, 60)
    parede7.x = 450
    parede7.y = 540
    physics.addBody( parede7, "static" )

    local sequenceDataPlayer = 
    {
        { name = "idle", frames = {2} }, 
        { name = "walkToRight", frames = {7, 8, 9, 7}, time = 333, loopCount = 0},
        { name = "walkToLeft", frames = {13, 15, 14, 13}, time = 333, loopCount = 0},
        { name = "jump", frames = {1} },
    }

    local sequenceDataZombie = 
    {
        {name = "idle", frames = {6}},
        {name = "walkToRight", frames = {1, 2, 3, 4, 5, 6}, time = 333, loopCount = 0},
        {name = "walkToLeft", frames = {8, 9, 10, 11, 12, 13}, time = 333, loopCount = 0},
    }

    local player = display.newSprite(sheet, sequenceDataPlayer)
    player.x = -300
    player.y = 950
    player:setSequence("idle")
    player:play()
    physics.addBody( player, "dynamic", {radius = 45, friction = 3, bounce = 0.1})
    player.isFixedRotation = true
    player.myName = "player"

    local zombie = display.newSprite(sheetZombie, sequenceDataZombie)
    zombie.x = 600
    zombie.y = 950
    physics.addBody(zombie, "dynamic", {radius = 45, density = 10, friction = 3, bounce = 0.1})
    zombie.isFixedRotation = true
    zombie.myName = "zombie"
    toRight = true
    toLeft = false
    if toRight == true then
        zombie:setSequence("walkToRight")
        zombie:play()
        zombie.x = zombie.x + 10
        timer.performWithDelay(3000, invertSequence)
    elseif toLeft == true then
        zombie:setSequence("walkToLeft")
        zombie:play()
        zombie.x = zombie.x - 10
        timer.performWithDelay(3000, invertSequence)
    end

    local buttons = {}

    buttons[1] = display.newImage("seta.png") --direita
    buttons[1].x = 1000
	buttons[1].y = 950
	buttons[1].rotation = 90
    buttons[1].myName = "right"
    buttons[1].alpha=0.5

    buttons[2] = display.newImage("seta.png") --esquerda
    buttons[2].x = 800
	buttons[2].y = 950
    buttons[2].rotation = -90
    buttons[2].myName = "left"
    buttons[2].alpha=0.5

    buttons[3] = display.newImage("seta.png") --cima
    buttons[3].x = -300
    buttons[3].y = 950
    buttons[3].rotation= 0
    buttons[3].myName = "up"
    buttons[3].alpha=0.5

    local walkX = 0
    local walkY = 0

    local function touchFunction(e)
        local phase = event.phase
		local name = event.keyName
        if e.phase == "began" or e.phase == "moved" then
            if e.target.myName == "up" then
                player:setSequence("jump")	
                player:applyLinearImpulse( 0, -100)
            elseif e.target.myName == "left" then
                player:setSequence("walkToLeft")
                player:play()
                walkX = -4
                player.xScale = -1
            elseif e.target.myName == "right" then
                player:setSequence("walkToRight")
                player:play()
                walkX = 4
                player.xScale = -1
            end
        else 

            if e.target.myName == "right" then
                player:setSequence("idle")
                
            elseif e.target.myName == "left" then
                player:setSequence("idle")
            end
            walkY=0
            walkX=0
        end
        
    end

    local j=1

    for j=1, #buttons do
        buttons[j]:addEventListener("touch", touchFunction)
        if j == 3 then
            buttons[j]:addEventListener("tap", touchFunction)
        end
    end
    local update = function(e)
        player.x = player.x + walkX
        player.y = player.y + walkY
    end
    porta.collision = goToNextLevel
    porta:addEventListener( "collision" )
    
    Runtime:addEventListener("enterFrame", update)
    sceneGroup:insert(background)
    sceneGroup:insert(parede2)
    sceneGroup:insert(parede6)
    sceneGroup:insert(parede7)
    sceneGroup:insert(porta)
end

function scene:show( event )
	local sceneGroup = self.view
	local phase = event.phase
	
	if phase == "will" then
		
	elseif phase == "did" then
		
		physics.start()
	end
end

function scene:hide( event )
	local sceneGroup = self.view
	
	local phase = event.phase
	
	if event.phase == "will" then
        physics.stop()
        sceneGroup:remove(background)
        display.remove(parede1)

        sceneGroup:remove(parede2)
        sceneGroup:remove(parede6)
        sceneGroup:remove(parede7)
        sceneGroup:remove(porta)
	elseif phase == "did" then
		-- Called when the scene is now off screen
	end	
	
end

function scene:destroy( event )	
end

---------------------------------------------------------------------------------

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-----------------------------------------------------------------------------------------

return scene
-- Bibliotecas

local composer = require( "composer" )
local scene = composer.newScene()
local physics = require "physics"
local widget = require "widget"

-- Algumas Configurações
local w = display.contentWidth
local h = display.contentHeight

local sheetData = { width=83, height=121, numFrames=24}

local sheet = graphics.newImageSheet("percy.png.", sheetData)

local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX

-- Inicio do jogo
function scene:create( event )
    physics.start()
    physics.setGravity(0, 9)
    local background = display.newImageRect( "game_background3.jpg", screenW, screenH )
	background.anchorX = 0 
    background.anchorY = 0
    background.x = 0 + display.screenOriginX 
    background.y = 0 + display.screenOriginY
    
    local parede1 = display.newImageRect("grass.png", 5000, 100)
    parede1.x = 60
    parede1.y = 1024
    physics.addBody( parede1, "static" )

    borda1 = display.newRect(-700, 0, 20, 5000)
    borda1.alpha = 0
    physics.addBody( borda1, "static" )

    borda1 = display.newRect(1450, 0, 20, 5000)
    borda1.alpha = 0
    physics.addBody( borda1, "static" )

    local sequenceData = 
    {
        { name = "idle", frames = {2} }, 
        { name = "walkToRight", frames = {7, 9, 8, 7}, time = 333, loopCount = 0},
        { name = "walkToLeft", frames = {13, 15, 14, 13}, time = 333, loopCount = 0},
        { name = "jump", frames = {1} },
    }

    local player = display.newSprite(sheet, sequenceData)
    player.x = -300
    player.y = 900
    player:setSequence("idle")
    player:play()
    physics.addBody( player, "dynamic", {radius = 25, friction = 3, bounce = 0.1})
    player.isFixedRotation = true
    player.myName = "player"

    local buttons = {}

    buttons[1] = display.newImage("seta.png") --direita
    buttons[1].x = 1000
	buttons[1].y = 950
	buttons[1].rotation = 90
    buttons[1].myName = "right"
    buttons[1].alpha=0.5

    buttons[2] = display.newImage("seta.png") --esquerda
    buttons[2].x = 800
	buttons[2].y = 950
    buttons[2].rotation = -90
    buttons[2].myName = "left"
    buttons[2].alpha=0.5

    buttons[3] = display.newImage("seta.png") --cima
    buttons[3].x = -300
    buttons[3].y = 950
    buttons[3].rotation= 0
    buttons[3].myName = "up"
    buttons[3].alpha=0.5

    local walkX = 0
    local walkY = 0

    local function touchFunction(e)
        local phase = event.phase
		local name = event.keyName
        if e.phase == "began" or e.phase == "moved" then
            if e.target.myName == "up" then
                player:setSequence("jump")	
                player:applyLinearImpulse( 0, -100)
            elseif e.target.myName == "left" then
                player:setSequence("walkToLeft")
                player:play()
                walkX = -4
                player.xScale = -1
            elseif e.target.myName == "right" then
                player:setSequence("walkToRight")
                player:play()
                walkX = 4
                player.xScale = -1
            end
        else 
            if e.target.myName == "right" then
                player:setSequence("idle")
                
            elseif e.target.myName == "left" then
                player:setSequence("idle")
            end
            walkY=0
            walkX=0
        end
        
    end

    local j=1

    for j=1, #buttons do
        buttons[j]:addEventListener("touch", touchFunction)
        if j == 3 then
            buttons[j]:addEventListener("tap", touchFunction)
        end
    end
    local update = function(e)
        player.x = player.x + walkX
        player.y = player.y + walkY
    end

    Runtime:addEventListener("enterFrame", update)
end

function scene:show( event )
	local sceneGroup = self.view
	local phase = event.phase
	
	if phase == "will" then
		-- Called when the scene is still off screen and is about to move on screen
	elseif phase == "did" then
		-- Called when the scene is now on screen
		-- 
		-- INSERT code here to make the scene come alive
		-- e.g. start timers, begin animation, play audio, etc.
		physics.start()
	end
end

function scene:hide( event )
	local sceneGroup = self.view
	
	local phase = event.phase
	
	if event.phase == "will" then
        physics.stop()
	elseif phase == "did" then
		-- Called when the scene is now off screen
	end	
	
end

function scene:destroy( event )	
end

---------------------------------------------------------------------------------

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-----------------------------------------------------------------------------------------

return scene


[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,601 posts
  • Corona SDK

With any force you want to take the object's mass into account.  That way, if you later change the size of the object you will still get the same response to any force.

player:applyLinearImpulse( 0 * player.mass, -100 * player.mass )




[topic_controls]
[/topic_controls]