collision help

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
newcoder
Posts: 10
Joined: Fri Dec 22, 2006 6:28 am

collision help

Post by newcoder »

i keep getting errors and i know its probably something small so could someone just check my code so that i can get done with the collision. (im terrible at collision).
heres my code:

Code: Select all

System.usbDiskModeActivate() 

red = Color.new(225,0,0)
player = Image.load("player.png")
background = Image.load("background.png")
pic1 = Image.load("pic1.png")
pic2 = Image.load("pic2.png")
Pic = { }
Pic[1] = { x= 80, y = 95, height = pic1:height(), width = pic1:width() }
Player = { }
Player[1] = { x = 50, y = 107 } 
screenwidth = 480 - player:width()
screenheight = 272 - player:width()
playerHeight = 90
playerWidth = 90
function collisionCheck(Pic)
if &#40;Player&#91;1&#93;.x + playerWidth > Pic&#91;1&#93;.x&#41; and &#40;Player&#91;1&#93;.x < Pic&#91;1&#93;.x + Pic&#91;1&#93;.width&#41; and &#40;Player&#91;1&#93;.y + playerHeight > Pic&#91;1&#93;.y&#41; and &#40;Player&#91;1&#93;.y < Pic&#91;1&#93;.y + Pic&#91;1&#93;.height&#41; then
Player&#91;1&#93;.x = oldx
Player&#91;1&#93;.y = oldy
end 
end
function movePlayer&#40;&#41;
pad = Controls.read&#40;&#41;
if pad&#58;left&#40;&#41; then 
Player&#91;1&#93;.x = Player&#91;1&#93;.x - 1 
end
if pad&#58;right&#40;&#41; then 
Player&#91;1&#93;.x = Player&#91;1&#93;.x + 1 
end
if pad&#58;up&#40;&#41; then 
Player&#91;1&#93;.y = Player&#91;1&#93;.y - 1 
end
if pad&#58;down&#40;&#41; then 
Player&#91;1&#93;.y = Player&#91;1&#93;.y + 1 
end
end
while true do
pad = Controls.read&#40;&#41;
oldx = Player&#91;1&#93;.x
oldy = Player&#91;1&#93;.y
screen&#58;clear&#40;&#41;
collisionCheck&#40;Pic&#91;1&#93;&#41;
screen&#58;blit&#40;0,0,background&#41;
screen&#58;blit&#40;Pic&#91;1&#93;.x,Pic1.y,pic1&#41;
screen&#58;blit&#40;140,107,pic2&#41;
screen&#58;blit&#40;Player&#91;1&#93;.x,Player&#91;1&#93;.y,player&#41;
if pad&#58;left&#40;&#41; and Player&#91;1&#93;.x > 0 then
Player&#91;1&#93;.x = Player&#91;1&#93;.x - 2
end

if pad&#58;right&#40;&#41; and Player&#91;1&#93;.x < screenwidth then
Player&#91;1&#93;.x = Player&#91;1&#93;.x + 2
end

if pad&#58;up&#40;&#41; and Player&#91;1&#93;.y > 0 then
Player&#91;1&#93;.y = Player&#91;1&#93;.y - 2
end

if pad&#58;down&#40;&#41; and Player&#91;1&#93;.y < screenheight then
Player&#91;1&#93;.y = Player&#91;1&#93;.y + 2
end
screen.waitVblankStart&#40;&#41;
screen.flip&#40;&#41;
end
everything worked up until i implemented collision so i know its something with that.
i you want to know the actual error it says:
attempt to index global field '?' a nil value
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

What line is the error on?
newcoder
Posts: 10
Joined: Fri Dec 22, 2006 6:28 am

Post by newcoder »

i think 15
newcoder
Posts: 10
Joined: Fri Dec 22, 2006 6:28 am

Post by newcoder »

anyone??
User avatar
harleyg
Posts: 123
Joined: Wed Oct 05, 2005 6:15 am

Post by harleyg »

1. "i think 15" is not a definite answer.
2. Learn to wait on these forums, do not expect answer straight away.
newcoder
Posts: 10
Joined: Fri Dec 22, 2006 6:28 am

Post by newcoder »

i know its just everytime i get to collision i end up dropping the project cause i cant get it to work. ill be more patient
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Nope it cant be 15. Just run the game and see what the error says, complete with line number and post it here. Maybe then we can help you
newcoder
Posts: 10
Joined: Fri Dec 22, 2006 6:28 am

Post by newcoder »

the error says this:
error: script.lua:15: attempt to index field '?' (a nil value)
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

And this is all of your code? That would mean it would be this line:

Code: Select all

playerWidth = 90 
Right?

I don't see anything wrong with that.
Kleptoone
Posts: 2
Joined: Fri Mar 02, 2007 8:18 am

Its the variable your passing into the collision function

Post by Kleptoone »

You're passing the specific index into the function and trying to index it again. you pass Pic[1] and then are trying to test Pic[1][1]. just pass the array without the one and it should work (at least compile)
Post Reply