News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

need help on shape collision...

Started by eatfishy, July 21, 2006, 08:36:54 PM

Previous topic - Next topic

eatfishy

Shape out of range??? I'm not sure am I using the shape editor incorrectly or the code is f up. Anyone know whats wrong wit it??

; PROJECT : rpgTesting
; AUTHOR  : uwdesign
; CREATED : 10/15/2004
; EDITED  : 7/21/2006
; ---------------------------------------------------------------------
#include "level.pba"
#include "shape"

openscreen 1024,768,16,2
setfps 200

dim Image(500)

`LoadAnimation(image(),fileName,startImage,endImage,frameWidth,frameHeight,transColor)
LoadAnimation(image(),"player.bmp",1,8,128,128,RGB(0,0,0))
LoadAnimation(image(),"boss1.bmp",401,403,128,256,RGB(0,0,0))

type character
x#
y#
speed#
distance
frame
attack$
jump$
direction$
endtype

dim player as character
player.x#=200
player.y#=576
player.speed#=1.5
player.distance=0
player.frame=image(1)
player.attack$="inactive"
player.jump$="inactive"
player.direction$="right"

global walkTime
global attackTime
global jumpTime#
global jump$="up"

global event$="false"


loadimage "tiles.bmp",1000
 
constant tileWidth=64
constant tileHeight=64
constant totalTiles = 4
constant levelWidth = 80
constant levelHeight =12

myMap=newmap(1)

rem PB image index starts at 0 when using makemapgfx
makemapgfx myMap,1000,tileWidth,tileHeight,totalTiles,rgb(0,0,0)

level$="forest"
CreateLevel MyMap,1,levelWidth,levelHeight
For Ylp=0 To GetLevelHeight(mymap,1)-1
For Xlp=0 To GetLevelWidth(MyMap,1)-1
 PokeLevelTile MyMap,1,Xlp,ylp,readdata()
Next xlp
Next ylp  

createcamera 1
positioncamera 1,0,0
capturetoscene

createsprite 1
spriteimage 1,player.frame

shape=5
`loadshape "boss1.shape",shape
filename$="C:\Program Files\PlayBasic\Projects\RPG_TESTING\testShape.shape"
loadshape filename$,shape
imageshape image(403),shape

createsprite 100
spriteimage 100,image(403)
spritecollision 100,on
spritecollisionmode 100,3
SpriteCollisionClass 100,%0001

positionsprite 100,900,448



testHit=0
kill=0

loadimage "swordCollision.bmp",20
createsprite 20
spritecollision 20,on
spritecollisionmode 20,1
SpriteCollisionClass 20,%0001

loadimage "door.bmp",30
createsprite 30
spriteimage 30,30


loadsound "attack.wma",1
soundvolume 1,255
forest=loadnewmusic("forest.wma")
playmusic forest
MusicVolume forest,255

x#=1024
y#=576

positionsprite 30,x#,y#


do
`cls 0
`DrawShape 1,1,1,1
if getmusicplaying(forest)=0 then playmusic forest
drawmap MyMap,1,0,00  
drawcamera 1
gosub PlayerControl
gosub AI
gosub Objects
gosub Collision
gosub Debug

sync
loop

Objects:
if player.distance=100 then obj=1
if obj=1
 drawsprite 30
 positionsprite 30,x#,y#
 curCam=getcamerax(1)
 if player.attack$="active" and player.jump$="active" then moveObj=1
 if rightkey()=1 and leftkey()=0 and player.x>=401 and player.speed#>0 then x#=x#-1.5
endif


return

AI:
if player.distance=1000 then event$="true"
if event$="true"
 drawsprite 100
endif  
return

Collision:
drawsprite 20
spriteimage 20,20
positionsprite 20,player.x#+76,player.y#+43
`positionsprite 20,mousex(),mousey()

if spritesoverlap(20,100) and player.attack$="active" then s=1
if s=1 and player.attack$="inactive" then s=0:kill=kill+1
if kill=>3 then event$="false":positionsprite 100,1000,1000


   
return

PlayerControl:
drawsprite 1
spriteimage 1,player.frame
positionsprite 1,player.x#,player.y#
PlayerWalk()
PlayerAttack()
PlayerJump()
return

Debug:
text 10,100,"FPS=" + str$(fps())
text 10,110,"Camera Position X="+str$(getcamerax(1))
text 10,120,"Player Frame="+str$(player.frame)
text 10,130,"Player Position X="+str$(player.x#)
text 10,140,"Player Distance="+str$(player.distance)
text 10,150,"Player Attack="+player.attack$
text 10,160,"Player Jump Status="+player.jump$
text 10,170,"Player Jump="+jump$
text 10,180,"Player Speed="+str$(player.speed#)
text 500,100,"LevelWidth="+str$(getlevelwidth(myMap,1))
text 500,110,"Shape Index="+str$(shape)
text 500,120,"hit="+str$(kill)
return

function PlayerWalk()
if leftkey()=1 and rightkey()=0
 player.x#=player.x#-player.speed#
 if player.jump$="inactive" then player.direction$="left"
 PlayerWalkAnimation("left")
 `movecamera 1,-(player.speed#),0
endif

if rightkey()=1 and leftkey()=0
 if player.x#<401 or event$="true" then player.x#=player.x#+player.speed#
 if player.jump$="inactive" then player.direction$="right"
 PlayerWalkAnimation("right")
 if player.x#=>401 and player.speed#>0 and event$="false" then _
  player.distance=player.distance+1:movecamera 1,player.speed#,0
endif
endfunction

function PlayerAttack()
if scancode()=44 then player.attack$="active"
if (player.attack$="active" and scancode()<>44)
 if player.jump$="inactive" then player.speed#=0
 Select player.direction$
  attackTime=attackTime+1  
  case "right"
   player.frame=image(3)
  case "left"
   player.frame=image(6)
 endselect
 if attackTime=25
  playsound 1
  if player.direction$="right" and player.jump$="inactive" then player.frame=image(1)
  if player.direction$="left" and player.jump$="inactive" then player.frame=image(8)
  if player.direction$="right" and player.jump$="active" then player.frame=image(4)
  if player.direction$="left" and player.jump$="active" then player.frame=image(5)
  player.attack$="inactive":attackTime=0
  player.speed#=1.5
 endif

endif
endfunction

function PlayerJump()

if scancode()=45 then player.jump$="active"
if player.jump$="active"
 if player.y#=528 and player.direction$="right" then player.frame=image(4)
 if player.y#=528 and player.direction$="left" then player.frame=image(5)
 select jump$
 case "up"
  player.y#=player.y#-jumpTime#
      if player.y#=<576 and player.y#=>526 then jumpTime#=3.0
      if player.y#=<526 and player.y#=>476 then dec jumpTime#=2.00
      if player.y#=<476 and player.y#=>426 then dec jumpTime#=1
      if player.y#=<400 then jump$="down":jumpTime#=4.0
 case "down"
  player.y#=player.y#+jumpTime#
  if player.y#>=576
   player.y#=576:player.jump$="inactive"
   jump$="up":jumpTime#=3
   Select player.direction$
    case "right"
     player.frame=image(1)
    case "left"
     player.frame=image(8)
   endselect
  endif
 endselect
endif
endfunction

function PlayerWalkAnimation(direction$)
walkTime=walkTime+player.speed#
Select direction$
 case "right"
  if walkTime>20 and player.jump$="inactive" then player.frame=image(1)
  if walkTime>40 and player.jump$="inactive" then player.frame=image(2)
  if walkTime>60 then walkTime=0
 case "left"
  if walkTime>20 and player.jump$="inactive" then player.frame=image(8)
  if walkTime>40 and player.jump$="inactive" then player.frame=image(7)
  if walkTime>60 then walkTime=0
endselect
endfunction

Function LoadAnimation(image(),File$,startImage,endImage,FrameWidth,FrameHeight,TransparentColour)
 index=LoadNewImage(file$)
 width=GetImageWidth(index)
 height=GetImageHeight(index)

 For i=startImage To endImage
  RenderToImage Index
  FreeImage=GetFreeImage()
  image(i)=FreeImage
  GetImage FreeImage,Xpos,Ypos,Xpos+Framewidth,Ypos+FrameHeight
  ImageMaskColour FreeImage,TransparentColour
  Xpos=Xpos+Framewidth
  If Xpos=>width Then Xpos=0: Ypos=Ypos+FRameHeight
 Next

 RenderToScreen
 DeleteImage index
EndFunction

Rem must match level width and heigth above
remstart
data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
data 1,2,3,3,1,2,1,1,1,1,1,2,1,1,1,1,2,1,1,1,1,2,3,3,1,2,1,1,1,1,1,2,1,1,1,1,2,1,1,1,1,2,3,3,1,2,1,1,1,1,1,2,1,1,1,1,2,1,1,1,1,2,3,3,1,2,1,1,1,1,1,2,1,1,1,1,2,1,1,1
data 1,3,1,1,1,3,1,1,1,1,1,2,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
data 1,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
data 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
remend



Here is the file:

DOWNLOAD my File

kevin

Sounds like the shape didn't load.  Therefore later on when the collision engine goes to check the sprite images associated shape(s), the calls to the shape library will pop a runtime error if that shape doesn't exist.

eatfishy

Okay..after couple of headache...I realized 2 things now.

1- When I use the shape editor, I thought it would save the extension for me automatically. I thought wrong... "You must type in. shape after the filename"

2- The fatal runtime error is a lil misleading for me. Because the shape was never loaded in, shouldn't the error pop up during the "LoadShape " event and not during the "drawShape or shapehitSprite"?

kevin

1) In PlayShape V0.05 the file extension and possible save name are already provided in the filename field.

2) LoadShape is a User Function.