在电晕SDK中拖动物理对象 [英] Drag physic object in corona sdk

查看:95
本文介绍了在电晕SDK中拖动物理对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在场景中拖动一个重力= 0,0的动态物体,我有一个物体类型为动态的正方形,以及一个物体类型为静态的图像,但是当将正方形拖动到图像上时,会有一个力很小,但可以超出图像并像图像一样传递到另一侧:

I'm try to drag a dynamic body with gravity = 0,0 in my scene, I have a square with body type dynamic, and an image with body type static, but when drag the square over the image this have a little force but can exceed the image and pass to the other side like the images:

这是我拖动正方形的代码:

This is my code to drag the square:

  local function dragBody( event )
                  local body = event.target
        local phase = event.phase
        local stage = display.getCurrentStage()

        if "began" == phase then
                stage:setFocus( body, event.id )
                body.isFocus = true
                body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )

        elseif body.isFocus then
                if "moved" == phase then
                        body.tempJoint:setTarget( event.x, event.y )

                elseif "ended" == phase or "cancelled" == phase then
                        stage:setFocus( body, nil )
                        body.isFocus = false
                        body.tempJoint:removeSelf()

                end
        end
        return true
end

这是创建对象的代码:

function scene:createScene( event )
    local group = self.view
    my_square = display.newImage("square.png")
    my_square.x = 60
    my_square.y = 60
    physics.addBody(my_square, "dynamic" )
    group:insert(my_square)

    floor = display.newImage("piso.png")
    floor.x = 160
    floor.y = 240
    physics.addBody(floor, "static" )
    group:insert(floor)   
end

感谢帮助.

推荐答案

首先,我建议您尝试:

physics.setContinuous( false )

如果您已经这样做了:

Physics2D引擎中有3种不同的物理类型.出于拖动目的,可以使用运动"对象类型.但是,如果有义务使用动态对象作为可拖动对象,则冲突中可能会出现错误.但是,如果您的静态对象每次都相同,则可以通过拖动功能对其进行控制.

There are 3 different physics type in Physics2D engine. For drag purposes you can use "Kinematic" object type. But if its an obligation to use a Dynamic object as a dragable object, there may be bugs in collisions. But if your static object will be same every time, you can control it in drag function.

我已经使用您想要实现的功能实现了一个小型手机游戏.链接在这里: https://itunes.apple.com/tr/app/bricks -world/id602065172?mt = 8

I have implemented a little mobile game using same thing that you want to achieve. Here is the link: https://itunes.apple.com/tr/app/bricks-world/id602065172?mt=8

如果您认为您想要这款游戏中的类似产品,请发表评论^^,我可以提供进一步的帮助.

If you think that you want something similar in this game, just leave a comment^^ I can help further.

P.S:在游戏中,操纵杆是动态的,屏幕周围的墙壁是静态的.

P.S : In the game, the controller paddle is dynamic and walls around the screen are static.

另一种解决方案:

local lastX, lastY
local function dragBody( event )
                  local body = event.target
        local phase = event.phase
        local stage = display.getCurrentStage()

        if "began" == phase then
                stage:setFocus( body, event.id )
                body.isFocus = true
                lastX, lastY = body.x, body.y
        elseif body.isFocus then
                if "moved" == phase then
                        -- You can change 1's with another value.
                        if(event.x > lastX) body.x = body.x + 1
                        else body.x = body.x - 1

                        if(event.y > lastY) body.y = body.y + 1
                        else body.y = body.y - 1

                        lastX, lastY = body.x, body.y

                elseif "ended" == phase or "cancelled" == phase then
                        stage:setFocus( body, nil )
                        body.isFocus = false
                end
        end
        return true
end

这篇关于在电晕SDK中拖动物理对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆