Okay, I'm having trouble implementing Wave's DeltaTime type. I thought I followed his instructions exactly, but I'm very new at this so who knows?
If my implementation is correct, then maybe the type itself is buggy?
main program:
Pretty much the only lines that refer to DeltaTime are these two lines from the update method of TypeActor:
x = x + (xMove*Delta.Time())
y = y + (yMove*Delta.Time())
For some reason, the turtle moves but the snake does not. Also, when the turtle hits the left side of the screen, it freezes. The player never moves. If I take out the reference to DeltaTime(), everything works as expected.
Here are the contents of FPS.bmx and DeltaTime.bmx, just in case I screwed something up while transcribing them:
FPS.bmx:
Delta Time
(Yes, I'm probabaly going to keep asking questions every step of the way on this simple little demo game until I figure out what the heck I'm doing.)
If my implementation is correct, then maybe the type itself is buggy?
main program:
Strict Include "DeltaTime.bmx" Include "FPS.bmx" Delta.Start() Type typeImageList Field image:TImage Field name:String Global imageList:TList Function Add(newFileName:String, newName:String ) Local newImage:typeImageList newImage = New typeImageList newImage.image = LoadImage(newFileName) newImage.name = newName If imageList = Null Then imageList = CreateList() End If ListAddLast(imageList,newImage) End Function Function Get:TImage(searchName:String) Local currentImageList:typeImageList Local foundImage:TImage If imageList <> Null Then For currentImageList = EachIn imageList If currentImageList.name = searchName Then foundImage = currentImageList.image End If Next End If Return foundImage End Function End Type Type typeSprite Field image:TImage Field x Field y Method Draw() DrawImage(image,x,y) End Method End Type Type typeActor Extends typeSprite Field xMove Field yMove Field rotDelta Method Update() x = x + (xMove*Delta.Time()) y = y + (yMove*Delta.Time()) End Method End Type Type typePlayer Extends typeActor Field health Method New() image = typeImageList.Get("Hero") End Method Method GetInputs() If KeyDown(KEY_LEFT) xMove:-1 If KeyDown(KEY_RIGHT) xMove:+1 If KeyDown(KEY_UP) yMove:-1 If KeyDown(KEY_DOWN) yMove:+1 End Method End Type Type typeEnemy Extends typeActor Field scoreValue ' how many points this enemy is worth Method New() scoreValue = 20 ' all enemies have a default score value End Method End Type Type typeEnemyTurtle Extends typeEnemy Method New() image = typeImageList.Get("Turtle") End Method End Type Type typeEnemySnake Extends typeEnemy Method New() image = typeImageList.Get("Snake") scoreValue = 30 End Method End Type Global imageEnemies:TImage[2] Global RefreshRate=30 'Hz = FPS Graphics 640,480,0,RefreshRate loadImages() playGame() End Function loadImages() ' Here you could read a text file to get the 2 strings ' needed to create the named images typeImageList.Add("images/hero.png","Hero") typeImageList.Add("images/turtle.png","Turtle") typeImageList.Add("images/snake.png","Snake") End Function Function playGame() Local player:typePlayer Local enemyList:TList enemyList = CreateList() player = New typePlayer player.x = 100 player.y = 50 Local enemy:typeEnemy ' create 1 turtle enemy = New TypeEnemyTurtle enemy.x = 200 enemy.y = 200 enemy.xMove = -1 ' moving left ListAddLast(enemyList, enemy) ' create 1 snake enemy = New TypeEnemySnake enemy.x = 240 enemy.y = 200 enemy.xMove = 1 ' moving right ListAddLast(enemyList, enemy) While Not KeyHit(KEY_Escape) Cls Delta.Update() player.GetInputs() player.Update() For enemy = EachIn enemyList enemy.Update() Next player.Draw() For enemy = EachIn enemyList enemy.Draw() Next FPS.Display() Flip Wend End Function
Pretty much the only lines that refer to DeltaTime are these two lines from the update method of TypeActor:
x = x + (xMove*Delta.Time())
y = y + (yMove*Delta.Time())
For some reason, the turtle moves but the snake does not. Also, when the turtle hits the left side of the screen, it freezes. The player never moves. If I take out the reference to DeltaTime(), everything works as expected.
Here are the contents of FPS.bmx and DeltaTime.bmx, just in case I screwed something up while transcribing them:
FPS.bmx:
Type FPS ' FPS_Counter <> Runs and displays the FPS Global Counter, Time, TFPS Function Calc%() Counter:+1 If Time < MilliSecs() TFPS = Counter' <- Frames/Sec Time = MilliSecs() + 1000'Update Counter = 0 EndIf Return TFPS EndFunction Function Display() DrawText "FPS: "+FPS.Calc(),10,10 End Function EndType
Delta Time
Type Delta 'Delta Time code provided by Truplos@.... Global DeltaTime# Global TimeDelay% Function Start() 'Call Delta.Start() before main loop TimeDelay = MilliSecs() End Function Function Time#() 'EXAMPLE: 'use this: Speed:+ 10*Delta.Time() 'instead of this: Speed:+ 10 'WTF is it +Delta.Time() or *Delta.Time()? The pdf says to multiply but that breaks my game! Return DeltaTime# End Function Function Update() 'Call Delta.Update() once in the main loop. DeltaTime = ( MilliSecs()- TimeDelay )*0.001 TimeDelay = MilliSecs() EndFunction End Type
(Yes, I'm probabaly going to keep asking questions every step of the way on this simple little demo game until I figure out what the heck I'm doing.)