0

ゲーム オブジェクトをタッチ位置に移動させようとしています。私はそれを行うためにさまざまな方法を試しましたが、すべて役に立ちませんでした。フレームごとにオブジェクトの y 位置を更新して、常に前進し続けるようにしていますが、これが力の追加に影響しているかどうかはわかりません。

カラスを移動する場所は次のとおりです (更新関数内)。

crow:translate((platformSpeed),0)

そして、カラスをタッチ位置に移動させようとしている場所は次のとおりです。

        local function attack(xPos,yPos)

            --get magnitude of touch vector
            local magnitude = math.sqrt(xPos*2 + yPos*2)

            --normalize vector
            xPos = xPos / magnitude
            yPos = yPos / magnitude

            print(xPos..yPos)

            local forceMag = 0.1 -- change this value to apply more or less force
            --now apply the force
            crow:setLinearVelocity(xPos*forceMag, yPos*forceMag)
            --crow:applyLinearImpulse(xPos*forceMag, yPos*forceMag, crow.x, crow.y)
        end

        local function touchHandler(event)

            if (event.phase == "began") then
            end

            if (event.phase == "ended") then
                if (event.yStart>event.y+10) then
                    jump()
                else attack(event.x,event.y) end
            end

        end

ご覧のとおり、私は線形速度と線形衝撃を試してきましたが、方向は常に間違っています!

どんな助けでも素晴らしいでしょう、ありがとう! アラン。

4

2 に答える 2

0

これは最近の質問に似ています。

最近の質問に対する回答があります。空白のプロジェクトでサンプル コードを確認して実行し、動作を確認してください。

私のコードは、物理学と線速度を使用しています。

https://stackoverflow.com/a/17847475/1605727

于 2013-07-25T01:43:20.473 に答える
0

これを試して:

local cow = display.newRect(0,0,50,50)  -- create your object

local trans
local function moveObject(e)
    if(trans)then
      transition.cancel(trans)
    end
    trans = transition.to(cow,{time=200,x=e.x,y=e.y})  -- move to touch position
end
Runtime:addEventListener("tap",moveObject)

コーディングを続ける.............. :)

于 2013-07-23T04:16:46.010 に答える