与 Corona 发生碰撞时不会发生碰撞

发布于 2024-12-13 02:48:23 字数 5277 浏览 2 评论 0原文

我遇到了一个新问题..我无法解决它..

我像一个球一样创建,当我单击时,球就会去我单击的地方。所以这没关系。 我正在填充一些飞机的框架,这些飞机会随着重力而下降。

但我有一个问题,因为当球和飞机接触时,我的碰撞不会触发......

有人可以帮助我吗?这是我的代码。

            --------------------------------------------------------
            -- CHARACTER PART --
            --------------------------------------------------------

            -- Creating the character --
            local _H = display.contentHeight;
            local _W = display.contentWidth;
            local character = display.newImageRect("images/small-black.png",80,80);
            character:setReferencePoint(display.CenterReferencePoint);
            character.x = _W/2; character.y = _H/2;
            character.color = "reddish";

            -- This is necessary so we know who hit who when taking care of a collision event
            character.name = "character"

            -- Listen to collisions
            character.collision = onCollision
            character:addEventListener("collision", character)

            -- Text to know the phase--
            local txt = display.newText("click state",0, 0, native.systemFont, 16*2);
            txt.xScale = 0.5; txt.yScale = 0.5;
            txt:setReferencePoint(display.CenterReferencePoint);
            txt.x = _W/2; txt.y = _H/2;

            -- Function on touch --
            function character:touch(e)
                if(e.phase == "began") then
                    txt.text = "Began Phase";
                    display.getCurrentStage():setFocus(e.target, e.id);
                elseif(e.phase =="moved") then
                    txt.text = "Moved phase";
                elseif(e.phase == "ended") then
                    txt.text = "Ended phase";
                    transition.to(self, {x = e.x, y = e.y, time=800});
                    transition.to(self, {x = e.x, y = e.y, time=800});
                end
            end

            -- Add event --
            character:addEventListener("touch", character);

            --------------------------------------------------------
            -- ENNEMY PART --
            --------------------------------------------------------

            -- Load and start physics
            local physics = require("physics")
            physics.start()

            -- Gravity --
            physics.setGravity(0, 20)


            -- Layers (Groups) --
            local gameLayer    = display.newGroup()
            local enemiesLayer = display.newGroup()

            -- Declare variables
            local gameIsActive = true
            local halfPlayerWidth
            local textureCache = {}
            textureCache[1] = display.newImage("images/enemy.png"); textureCache[1].isVisible = false;
            local halfEnemyWidth = textureCache[1].contentWidth * .5

            gameLayer:insert(enemiesLayer)


            -- Take care of collisions
            local function onCollision(self, event)

                -- Player collision - GAME OVER 
                if self.name == "character" and event.other.name == "enemy" and gameIsActive then

                    local gameoverText = display.newText("Game Over!", 0, 0, "HelveticaNeue", 35)
                    gameoverText:setTextColor(255, 255, 255)
                    gameoverText.x = display.contentCenterX
                    gameoverText.y = display.contentCenterY
                    gameLayer:insert(gameoverText)

                    -- This will stop the gameLoop
                    gameIsActive = false
                end
            end

            -- Add to main layer
            gameLayer:insert(character)


            -- Store half width, used on the game loop
            halfPlayerWidth = character.contentWidth * .5

            -- GAME LOOP --

            local  timeLastEnemy = 0, 0
            local function gameLoop(event)
                if gameIsActive then
                    -- Remove collided enemy planes
                    --for i = 1, #toRemove do
                        --toRemove[i].parent:remove(toRemove[i])
                        --toRemove[i] = nil
                    --end

                    -- Check if it's time to spawn another enemy,
                    -- based on a random range and last spawn (timeLastEnemy)
                    if event.time - timeLastEnemy >= math.random(600, 1000) then
                        -- Randomly position it on the top of the screen
                        local enemy = display.newImage("images/enemy.png")
                        enemy.x = math.random(halfEnemyWidth, display.contentWidth - halfEnemyWidth)
                        enemy.y = -enemy.contentHeight

                        -- This has to be dynamic, making it react to gravity, so it will
                        -- fall to the bottom of the screen.
                        physics.addBody(enemy, "dynamic", {bounce = 0})
                        enemy.name = "enemy"

                        enemiesLayer:insert(enemy)
                        timeLastEnemy = event.time
                    end

                end
            end

            -- Call the gameLoop function EVERY frame,
            -- e.g. gameLoop() will be called 30 times per second ir our case.
            Runtime:addEventListener("enterFrame", gameLoop)

I have got a new problem .. I can't fix it ..

I create like a ball, and when i click, the ball is going where i clicked . So this is allright .
I am populating with frames some planes wich are going down with the gravity .

But i have a problem because my collision is not firing when the ball and the plane touch themselves...

Can someone help me ? This is my code .

            --------------------------------------------------------
            -- CHARACTER PART --
            --------------------------------------------------------

            -- Creating the character --
            local _H = display.contentHeight;
            local _W = display.contentWidth;
            local character = display.newImageRect("images/small-black.png",80,80);
            character:setReferencePoint(display.CenterReferencePoint);
            character.x = _W/2; character.y = _H/2;
            character.color = "reddish";

            -- This is necessary so we know who hit who when taking care of a collision event
            character.name = "character"

            -- Listen to collisions
            character.collision = onCollision
            character:addEventListener("collision", character)

            -- Text to know the phase--
            local txt = display.newText("click state",0, 0, native.systemFont, 16*2);
            txt.xScale = 0.5; txt.yScale = 0.5;
            txt:setReferencePoint(display.CenterReferencePoint);
            txt.x = _W/2; txt.y = _H/2;

            -- Function on touch --
            function character:touch(e)
                if(e.phase == "began") then
                    txt.text = "Began Phase";
                    display.getCurrentStage():setFocus(e.target, e.id);
                elseif(e.phase =="moved") then
                    txt.text = "Moved phase";
                elseif(e.phase == "ended") then
                    txt.text = "Ended phase";
                    transition.to(self, {x = e.x, y = e.y, time=800});
                    transition.to(self, {x = e.x, y = e.y, time=800});
                end
            end

            -- Add event --
            character:addEventListener("touch", character);

            --------------------------------------------------------
            -- ENNEMY PART --
            --------------------------------------------------------

            -- Load and start physics
            local physics = require("physics")
            physics.start()

            -- Gravity --
            physics.setGravity(0, 20)


            -- Layers (Groups) --
            local gameLayer    = display.newGroup()
            local enemiesLayer = display.newGroup()

            -- Declare variables
            local gameIsActive = true
            local halfPlayerWidth
            local textureCache = {}
            textureCache[1] = display.newImage("images/enemy.png"); textureCache[1].isVisible = false;
            local halfEnemyWidth = textureCache[1].contentWidth * .5

            gameLayer:insert(enemiesLayer)


            -- Take care of collisions
            local function onCollision(self, event)

                -- Player collision - GAME OVER 
                if self.name == "character" and event.other.name == "enemy" and gameIsActive then

                    local gameoverText = display.newText("Game Over!", 0, 0, "HelveticaNeue", 35)
                    gameoverText:setTextColor(255, 255, 255)
                    gameoverText.x = display.contentCenterX
                    gameoverText.y = display.contentCenterY
                    gameLayer:insert(gameoverText)

                    -- This will stop the gameLoop
                    gameIsActive = false
                end
            end

            -- Add to main layer
            gameLayer:insert(character)


            -- Store half width, used on the game loop
            halfPlayerWidth = character.contentWidth * .5

            -- GAME LOOP --

            local  timeLastEnemy = 0, 0
            local function gameLoop(event)
                if gameIsActive then
                    -- Remove collided enemy planes
                    --for i = 1, #toRemove do
                        --toRemove[i].parent:remove(toRemove[i])
                        --toRemove[i] = nil
                    --end

                    -- Check if it's time to spawn another enemy,
                    -- based on a random range and last spawn (timeLastEnemy)
                    if event.time - timeLastEnemy >= math.random(600, 1000) then
                        -- Randomly position it on the top of the screen
                        local enemy = display.newImage("images/enemy.png")
                        enemy.x = math.random(halfEnemyWidth, display.contentWidth - halfEnemyWidth)
                        enemy.y = -enemy.contentHeight

                        -- This has to be dynamic, making it react to gravity, so it will
                        -- fall to the bottom of the screen.
                        physics.addBody(enemy, "dynamic", {bounce = 0})
                        enemy.name = "enemy"

                        enemiesLayer:insert(enemy)
                        timeLastEnemy = event.time
                    end

                end
            end

            -- Call the gameLoop function EVERY frame,
            -- e.g. gameLoop() will be called 30 times per second ir our case.
            Runtime:addEventListener("enterFrame", gameLoop)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2024-12-20 02:48:23

问题解决了!

不要忘记写Physics.addBody (..)

Problem fixed !

Don't forget to write physics.addBody ( .. )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文