| View previous topic :: View next topic |
| Author |
Message |
newcoder
Joined: 22 Dec 2006 Posts: 10
|
Posted: Tue Feb 20, 2007 12:47 am Post subject: collision help |
|
|
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: | 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 (Player[1].x + playerWidth > Pic[1].x) and (Player[1].x < Pic[1].x + Pic[1].width) and (Player[1].y + playerHeight > Pic[1].y) and (Player[1].y < Pic[1].y + Pic[1].height) then
Player[1].x = oldx
Player[1].y = oldy
end
end
function movePlayer()
pad = Controls.read()
if pad:left() then
Player[1].x = Player[1].x - 1
end
if pad:right() then
Player[1].x = Player[1].x + 1
end
if pad:up() then
Player[1].y = Player[1].y - 1
end
if pad:down() then
Player[1].y = Player[1].y + 1
end
end
while true do
pad = Controls.read()
oldx = Player[1].x
oldy = Player[1].y
screen:clear()
collisionCheck(Pic[1])
screen:blit(0,0,background)
screen:blit(Pic[1].x,Pic1.y,pic1)
screen:blit(140,107,pic2)
screen:blit(Player[1].x,Player[1].y,player)
if pad:left() and Player[1].x > 0 then
Player[1].x = Player[1].x - 2
end
if pad:right() and Player[1].x < screenwidth then
Player[1].x = Player[1].x + 2
end
if pad:up() and Player[1].y > 0 then
Player[1].y = Player[1].y - 2
end
if pad:down() and Player[1].y < screenheight then
Player[1].y = Player[1].y + 2
end
screen.waitVblankStart()
screen.flip()
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 |
|
| Back to top |
|
 |
Altair
Joined: 20 May 2006 Posts: 76 Location: The Netherlands
|
Posted: Tue Feb 20, 2007 2:08 am Post subject: |
|
|
| What line is the error on? |
|
| Back to top |
|
 |
newcoder
Joined: 22 Dec 2006 Posts: 10
|
Posted: Tue Feb 20, 2007 2:09 am Post subject: |
|
|
| i think 15 |
|
| Back to top |
|
 |
newcoder
Joined: 22 Dec 2006 Posts: 10
|
Posted: Tue Feb 20, 2007 9:12 am Post subject: |
|
|
| anyone?? |
|
| Back to top |
|
 |
harleyg

Joined: 05 Oct 2005 Posts: 123
|
Posted: Tue Feb 20, 2007 11:27 am Post subject: |
|
|
1. "i think 15" is not a definite answer.
2. Learn to wait on these forums, do not expect answer straight away. |
|
| Back to top |
|
 |
newcoder
Joined: 22 Dec 2006 Posts: 10
|
Posted: Tue Feb 20, 2007 11:57 am Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
Altair
Joined: 20 May 2006 Posts: 76 Location: The Netherlands
|
Posted: Wed Feb 21, 2007 4:46 am Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
newcoder
Joined: 22 Dec 2006 Posts: 10
|
Posted: Wed Feb 21, 2007 8:37 am Post subject: |
|
|
the error says this:
error: script.lua:15: attempt to index field '?' (a nil value) |
|
| Back to top |
|
 |
Altair
Joined: 20 May 2006 Posts: 76 Location: The Netherlands
|
Posted: Sat Feb 24, 2007 11:06 pm Post subject: |
|
|
And this is all of your code? That would mean it would be this line:
Right?
I don't see anything wrong with that. |
|
| Back to top |
|
 |
Kleptoone
Joined: 02 Mar 2007 Posts: 2
|
Posted: Fri Mar 02, 2007 8:20 am Post subject: Its the variable your passing into the collision function |
|
|
| 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) |
|
| Back to top |
|
 |
|