Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

check direction variable, move and repeat
Started by dwane dibbley Jan 27 2019 02:40 AM

2 replies to this topic
transition move variable
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dwane dibbley

[GLOBAL: userInfoPane.html]
dwane dibbley
  • Observer

  • 6 posts
  • Corona SDK

Please help. I've been scripting for a couple of months. I'm tring to move a object (man) in
transitions of 10 in the direction of the variable (direction). on completing its move, it checks the direction again
and repeats the process. the idea been that it can move correctly around a maze without hitting the walls...

For instance a player is moving the man up within the maze, they press move right...
after each transition a function checks all possible directions in can move in,
if it can't move right it keeps moving up until it has the option to move right.

I hope this makes some sort of sense, i'm just stuck on the move, check direction, repeat,

I can then manipulate the direction variable accordingly

 

i've tried many ideas, this is the latest:-

local direction
local newDirection
local man = display.newImageRect( "images/man.png" ,10 ,10 )
man.x=100; man.y=100

local right = display.newImageRect( "images/button.png" ,20 ,20 )
right.x=250; right.y=440
right.id = "rightButton"

local left = display.newImageRect( "images/button.png" ,20 ,20 )
left.x=200; left.y=440
left.id = "leftButton"

local function checkDirection( event )
    print (direction)
    if direction == 1 then
        newDirection = man.x
        newDirection = newDirection+10
        transition.moveto( man, { x=newDirection, y=0, time=200 } )    
    end

end

-- direction 1 = right / direction 2 = left
-- SET DIRECTION RIGHT
function right:touch( event )
    if event.phase == "began" then
        print( "You touched the right button!" )
        direction = 1
        return true
    end
end

-- SET DIRECTION LEFT
function left:touch( event )
    if event.phase == "began" then
        print( "You touched the left button!" )
        direction = 2
        return true
    end
end
 
right:addEventListener( "touch", right )
left:addEventListener( "touch", left )


Runtime:addEventListener( "enterFrame", checkDirection )


[TOPIC: post.html]
#2

dwane dibbley

[GLOBAL: userInfoPane.html]
dwane dibbley
  • Observer

  • 6 posts
  • Corona SDK

this script is a little closer

local direction
local newDirection
local man = display.newImageRect( "images/pill.png" ,10 ,10 )
man.x=100; man.y=100


local right = display.newImageRect( "images/wall.png" ,20 ,20 )
right.x=250; right.y=440
right.id = "rightButton"

local left = display.newImageRect( "images/wall.png" ,20 ,20 )
left.x=200; left.y=440
left.id = "leftButton"



local function move(direction)
    local function direct()
        print("direction:= "..direction)
        direction = direction
        move(direction)
    end
    
    if direction == 1 then
        newDirection = man.x
        newDirection = newDirection+10
        print ("move right")
        transition.moveTo( man, { x=newDirection, y=y, time=300, onComplete=direct} )
    elseif direction == 2 then
        newDirection = man.x
        newDirection = newDirection-10
        print ("move left")
        transition.moveTo( man, { x=newDirection, y=y, time=300, onComplete=direct} )
    end
end

-- direction 1 = right / direction 2 = left
-- SET DIRECTION RIGHT
function right:touch( event )
    if event.phase == "began" then
        print( "You touched the right button!" )
        direction = 1
        return true
    end
    move(direction)
end

-- SET DIRECTION LEFT
function left:touch( event )
    if event.phase == "began" then
        print( "You touched the left button!" )
        direction = 2
        return true
    end
    move(direction)
end
 
right:addEventListener( "touch", right )
left:addEventListener( "touch", left )


[TOPIC: post.html]
#3

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 616 posts
  • Corona SDK

Your code doesn't provide much help without understanding how the maze itself has been created.

I'm presuming that we are talking about a simple grid where the player can either pass a cell or not. I wrapped up a brief code sample that shows the approach that I would take.

Simply put, I'd start by always checking if the cell the player is trying to move to exists. If it does, then can the player pass through it? The maze in the sample contains cells with values 1 or 0 for passable and impassable. If the cell is passable, then just move the player there. If you want to keep on moving, then just add an onComplete that will repeat the movement.

The sample below is simplified and you'd need to add in your rules for how long to repeat, etc. but this should get you going again.
 

local maze = {
	{1,0,1,1,1},
	{1,0,1,0,0},
	{1,1,1,0,0},
	{1,0,0,0,0}
}

local tile = {}
local tileStartX, tileStartY, tileSize = 60, 60, 60

for i = 1, #maze do
	tile[i] = {}
	for j = 1, #maze[i] do
		tile[i][j] = display.newRect( tileStartX+tileSize*(j-1), tileStartY+tileSize*(i-1), tileSize, tileSize )
		if maze[i][j] == 1 then
			tile[i][j]:setFillColor(0,1,0)
		else
			tile[i][j]:setFillColor(1,0,0)
		end
	end
end

local locRow = 1
local locColumn = 1
local direction = "down"
local charTransition

local character = display.newCircle( tile[locRow][locColumn].x, tile[locRow][locColumn].y, 20 )
character:setFillColor(0,0,1)

local checkPath

local function moveTo(row,column)
	charTransition = transition.to( character, { time=500, x=tile[row][column].x, y=tile[row][column].y, onComplete=checkPath })
end

function checkPath()
	local toRow, toColumn = locRow, locColumn
	-- adjust the toRow/toColumn according to the direction
	if direction == "left" then
		toColumn = toColumn-1
	elseif direction == "right" then
		toColumn = toColumn+1
	elseif direction == "up" then
		toRow = locRow-1
	elseif direction == "down" then
		toRow = locRow+1
	end
	-- check if the toRow exists
	if maze[toRow] then
		-- check if the toColumn exists on the toRow
		if maze[toRow][toColumn] then
			-- check if the cell is passable and update the location
			if maze[toRow][toColumn] == 1 then
				locRow, locColumn = toRow, toColumn
				moveTo(toRow,toColumn)
			end
		end
	end
end

checkPath()



[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: transition, move, variable