PDA

View Full Version : Menu.lua



splodger15
November 6th, 2006, 16:47
I seem to be having trouble with my menu.lua ,here is my script.lua

------------------------------------------------------------------

-- Created by splodger15


----------detect other cars collision--------------
function detectCollision(player, opponent)
if (player.x + character:width() > opponent.x) and (player.x < opponent.x + opponent.pic:width()) and (player.y + character:height() > opponent.y) and (player.y < opponent.y + opponent.pic:height()) then
screen.waitVblankStart(60)
end
end

Room_width = 480
Room_height = 272
player1x = 15
player1y = 136
road=Image.load("images/road.PNG")
background=road
car1=Image.load("images/car1.PNG")
player1=car1
car2=Image.load("images/car2.PNG")
car3=Image.load("images/car3.PNG")
car4=Image.load("images/car4.PNG")

----------walls------------

wall1 = Image.createEmpty(480,2)
wall2 = Image.createEmpty(2,272)
wall3 = Image.createEmpty(2,272)
wall4 = Image.createEmpty(480,2)

wall1 = {x = 0, y = 0, height = wall1:height(), width = wall1:width() }
wall2 = {x = 450, y = 0, height = wall2:height(), width = wall2:width() }
wall3 = {x = 0, y = 0, height = wall3:height(), width = wall3:width() }
wall4 = {x = 0, y = 299, height = wall4:height(), width = wall4:width() }



player = {}
player.x = 250
player.y = 100
playerHeight = 63
playerWidth = 33

car2 = {}
car2.x = 128
car2.y = 90
car2Height = 63
car2Width = 33
car2.pic = Image.load("images/car2.PNG")

car3 = {}
car3.x = 228
car3.y = 90
car3Height = 63
car3Width = 33
car3.pic = Image.load("images/car3.PNG")

car4 = {}
car4.x = 28
car4.y = 20
car4Height = 63
car4Width = 33
car4.pic = Image.load("images/car4.PNG")

character = car1

------------------player sprite loading--------------

function rands1()
rand2 = math.random(1,3)
end

function rands2()
rand3 = math.random(1,3)
end

function rands3()
rand4 = math.random(1,3)
end

XScrollPosition = 480

numberOfScrolls = 0

while true do
math.randomseed(os.time() )
rand=math.random(1,3)
random1 =math.random(20,25)
random2 =math.random(90,95)
random3 =math.random(192,197)

pad = Controls.read()
screen:clear()

oldx = player.x
oldy = player.y
screen:clear()

if pad:left() then
character = car1
player.x = player.x - 2
end

if pad:right() then
character = car1
XScrollPosition = XScrollPosition - 10
if XScrollPosition < 0 then
XScrollPosition = XScrollPosition + 480
numberOfScrolls = numberOfScrolls + 1
end
end

if pad:up() then
character = car1
player.y = player.y - 2
end

if pad:down() then
character = car1
player.y = player.y + 2
end

if pad:triangle() then
dofile("index1.lua")
end

if pad:square() then
Image:save("screenshot.png")
end

if pad:start() then
break
end


if rand ==1 then
car2.x = car2.x -10
car3.x = car3.x-10
car4.x = car3.x-10
end

if rand ==2 then
car2.x = car2.x -10
car3.x = car3.x-10
car4.x = car3.x-10
end

if rand ==3 then
car2.x = car2.x -10
car3.x = car3.x-10
car4.x = car3.x-10
end

if car2.x <=0 then
car2.x =510
car2.y = random1
rands1()
end

if car3.x <=0 then
car3.x = 520
car3.y = random2
rands2()
end

if car4.x <=0 then
car4.x = 500
car4.y = random3
rands3()
end

screen:clear()

screen:blit(XScrollPosition, 0, road)

if XScrollPosition > 0 then
screen:blit(XScrollPosition - 480, 0, road)
else
screen:blit(XScrollPosition + 480, 0, road)
end

if numberOfScrolls > 30 then
dofile("eol.lua")
end

if player.x > 448 then
Player.x = 448
end
if player.x < 0 then
player.x = 0
end

if player.y > 240 then
player.y = 240
end
if player.y < 0 then
player.y = 0
end


detectCollision(player, car2)
detectCollision(player, car3)
detectCollision(player, car4)

-----------blit background and player------------

screen:blit(player.x, player.y, character)
screen:blit(car2.x, car2.y, car2.pic)
screen:blit(car3.x, car3.y, car3.pic)
screen:blit(car4.x, car4.y, car4.pic)
screen.flip()
screen.waitVblankStart()
end

------------------------------------------------------------------
and here is my menu.lua
------------------------------------------------------------------
title = Image.load("title.png")
while true do
pad = Controls.read()
if pad:cross() then
dofile("script.lua")
end
if pad:square() then
break
end
end

---------------------------------------------------

But when my game starts i have to hold down the square button for like 10 seconds then the game starts by the square button is meant to be the exit button can someone tell me how to fix this

Thanks

yaustar
November 6th, 2006, 19:02
The problem is that you are using dofiles in files that have been dofiled without fully understanding what happens. In short don't use dofiles unless you are 100% sure you know what it does.

To run through what it does, go through this little exercise (on a copy of the project). Start at the first script that is called. Everytime you hit a dofile call, copy the contents of the file that you are dofiling and replace the dofile call with it. eg

if 10 == X then
dofile("haha.lua")
end
becomes

if 10 == X then
-- what was in haha.lua
X = 40
end
Then start again from the top of the file. Keep going until there are no more dofile calls and post the code here again. You should end up with one script. If you don't, you are using dofiles incorrectly.

splodger15
November 6th, 2006, 19:14
I dont fully understand would it be something like

dofile("menu.lua")
------------------------------------------------------------------
--
title = Image.load("title.png")
while true do
pad = Controls.read()
if pad:cross() then
dofile("script.lua")
end
if pad:square() then
break
end
end

yaustar
November 6th, 2006, 19:18
Example:

--Blah.lua
if X == 10 then
dofile("Haha.lua")
end

-- Haha.lua
X =100
PrintSomeStuffToTheScreen()

Going through the exercise I gave, this would become

--Blah.lua
if X == 10 then
X =100
PrintSomeStuffToTheScreen()
end

splodger15
November 6th, 2006, 19:31
Do you have to do it that way or cant you do the "dofile"

yaustar
November 8th, 2006, 00:35
You can do the dofile but I was trying to show you why you are using it wrong. If you did that exercise, you would realise you never finish as you have cyclic dofile calls.