1

来年完成予定の学校でのプロジェクト用に RPG スタイルのゲームを作ろうとしています。コロナSDKを使っていて、しばらく情報を探していたら、D-PADの動作コードを見つけて適用しようとしたのですがうまくいきません。

こちらがmotion.luaです

-- D-PAD buttons
local buttonUp = display.newImage("images/button.png", 50, 300)
local buttonDown = display.newImage("images/button.png", 50, 375)
local buttonLeft = display.newImage("images/button.png", 50, 375)
local buttonRight = display.newImage("images/button.png", 50, 375)
local buttonLeftUp = display.newImage("images/buttonv2.png", 50, 375)
local buttonRightUp = display.newImage("images/buttonv2.png", 50, 375)
local buttonLeftDown = display.newImage("images/buttonv2.png", 50, 375)
local buttonRightDown = display.newImage("images/buttonv2.png", 50, 375)

local player = display.newImage("images/player.png")
player.width = 100
player.height = 60
player.y = 260
player.x = math.random(45, 175)
physics.addBody(player, "dynamic", {friction=0.5, bounce=0, gravity=0})

print(player.x)

--Objects and properties
buttonUp.x = display.contentWidth / 2
buttonUp.y = display.contentHeight - 212
buttonUp.alpha = 0.4
--

buttonDown.x = display.contentWidth / 2
buttonDown.y = display.contentHeight - 100
buttonDown.alpha = 0.4
buttonDown.rotation = 180
--

buttonLeft.x = (display.contentWidth / 2) - 56
buttonLeft.y = display.contentHeight - 156
buttonLeft.alpha = 0.4
buttonLeft.rotation = -90
--

buttonRight.x = (display.contentWidth / 2) + 56
buttonRight.y = display.contentHeight - 156
buttonRight.alpha = 0.4
buttonRight.rotation = 90
--

buttonLeftUp.x = (display.contentWidth / 2) + 56
buttonLeftUp.y = display.contentHeight - 100
buttonLeftUp.alpha = 0.4
buttonLeftUp.rotation = 90
--

buttonRightUp.x = (display.contentWidth / 2) + 56
buttonRightUp.y = display.contentHeight - 212
buttonRightUp.alpha = 0.4
--

buttonLeftDown.x = (display.contentWidth / 2) - 56
buttonLeftDown.y = display.contentHeight - 212
buttonLeftDown.alpha = 0.4
buttonLeftDown.rotation = -90
--

buttonRightDown.x = (display.contentWidth / 2) - 56
buttonRightDown.y = display.contentHeight - 100
buttonRightDown.alpha = 0.4
buttonRightDown.rotation = 180
--

--Variables that will be used
local motionx = 0 --X Motion
local motiony = 0 -- Y Motion
local speed = 5 -- Speed

local function stop (event) --Statements to control when there is no 
--touch and the touch is outside the D-Pad

        if "ended" == event.phase then --Check to see if the touch ended

        motionx = 0
        motiony = 0
        end

        --Touch Pos. Checkers

        if event.y < (display.contentHeight - 252) then

        motionx = 0
        motiony = 0

        end


        if event.y > (display.contentHeight - 70) then

        motionx = 0
        motiony = 0

        end

        if event.x > (display.contentWidth - 70) then

        motionx = 0
        motiony = 0

        end

        if event.x < 69 then

        motionx = 0
        motiony = 0

        end
    end

Runtime:addEventListener("touch", stop ) --What activates the stop function

----
local function move (event) -- Moves and updates the Player's movement 
--and the speed feedback
player.x = player.x + motionx;
player.y = player.y + motiony;

end


Runtime:addEventListener("enterFrame", move) --Every frame of the program will 
--run this     function

-----

local function wrap (event) --Function used to keep the player on screen

if player.x < -85 then
player.x = 405
end
----
if player.x > 405 then
player.x = -85
end
-------
if player.y < -88 then
player.y = 568
end
----
if player.y > 568 then
player.y = -88
end
---

if speed < 0 then

speed = 0
end


Runtime:addEventListener("enterFrame", wrap)

-------

--Button Functions to set the motionx and motiony variables 

function buttonUp:touch()
motionx = 0
motiony = -speed
end

buttonUp:addEventListener("touch", buttonUp)

------
function buttonDown:touch()
motionx = 0
motiony = speed

end

buttonDown:addEventListener("touch", buttonDown)
------

function buttonLeft:touch()
motionx = -speed
motiony = 0

end

buttonLeft:addEventListener("touch", buttonLeft)
------
function buttonRight:touch()
motionx = speed
motiony = 0

end

buttonRight:addEventListener("touch", buttonRight)
-------
-----

function buttonLeftUp:touch()
motionx = speed
motiony = speed

end

buttonLeftUp:addEventListener("touch", buttonLeftUp)


function buttonRightUp:touch()
motionx = speed
motiony = -speed

end

buttonRightUp:addEventListener("touch", buttonRightUp)

function buttonLeftDown:touch()
motionx = -speed
motiony = -speed

end

buttonLeftDown:addEventListener("touch", buttonLeftDown)

function buttonRightDown:touch()
motionx = -speed
motiony = speed

end

buttonRightDown:addEventListener("touch", buttonRightDown)
end

ここに main.lua ファイルがあります

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

-- Coding Tutorials
-- http://forums.coronalabs.com/topic/35372-rotation-problem-with-tap-movementrpg-like/
-- http://www.coronalabs.com/blog/2013/01/22/implementing-pinch-zoom-rotate/#comment-25508
-- http://forums.coronalabs.com/topic/35430-movements-in-rpg-game/
-- http://thatssopanda.com/corona-sdk-tutorials/moving-a-character-left-and-right-with-corona-sdk/
-- http://www.williammalone.com/articles/create-html5-canvas-javascript-game-character/images/character-color.png
-- http://www.youtube.com/watch?v=i_n3-ThKrtw

-- Useful information
-- http://www.coronalabs.com/resources/3rd-party-tools-and-services/
-- http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html

-- Sprites
-- http://opengameart.org/content/700-sprites

-- Sprite sheets and  Sprite sequences
-- http://www.coronalabs.com/blog/2013/05/14/sprites-and-image-sheets-for-beginners/
-- http://docs.coronalabs.com/api/library/sprite/index.html

-- Code Examples
-- http://mobile.tutsplus.com/tutorials/corona/creating-a-scrolling-background-with-corona-sdk/

-- Movement D-PAD
-- http://developer.coronalabs.com/code/d-pad-demo

-- Collision detection
-- http://developer.coronalabs.com/content/game-edition-collision-detection
-- http://developer.coronalabs.com/code/physics-body-collision-location
-- http://forums.coronalabs.com/topic/32706-get-actual-collision-xy/

-- Physics
-- http://docs.coronalabs.com/api/library/physics/

-- Inventory system
-- http://howto.oz-apps.com/2011/07/inventory-management-system-for-games.html
-- http://forums.coronalabs.com/topic/29697-inventory-system-for-adventure-game/
-----------------------------------------------------------------------------------------

-- Requirements

-- Set Variables
_W = display.contentWidth;
_H = display.contentHeight;
motionx = 0;
speed = 2;

--Collision filters
local playerCollisionFilter = {categoryBits = 1, maskBits = 2}
local objectsCollision = {categoryBits = 2, maskBits = 1 }

-- Start Physics
local physics = require("physics")

physics.start();
physics.setGravity(0,0)


--background
local backgroundimage = display.newImage("images/bg.png")
backgroundimage.x = 175; backgroundimage.y = 180
backgroundimage.x = "center"

require("movement")

function scrollCity(self, event)
    if self.x < -429 then
        self.x = self.x
        self.x = 100
    else
        self.x = self.x
    end
end

backgroundimage.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", backgroundimage)

私が持っている唯一の問題は、プレーヤーの動きです。事前にいくつかの助けに感謝します。

4

1 に答える 1

1

各ボタンのコードについては、プレーヤーが event.phase に基づいてのみ移動することをお勧めします。ボタンを押したまま離すには、別の条件が必要です。ここでは、開始するための変更を示します。

function buttonUp:touch(event)
    if event.phase == "began" then
       motionx = 0
       motiony = -speed
    return true
    elseif event.phase == "ended" then
       -- some condition for if you let go
       motionx = 0; motiony = 0; -- I assume you'd want to stop
    end
end

buttonUp:addEventListener("touch", buttonUp)
于 2013-12-17T08:51:31.580 に答える