Help: Sprite Type: Tying graphics to instances?

BlitzMax Forums/BlitzMax Beginners Area/Help: Sprite Type: Tying graphics to instances?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

OK.. I extended to original code to have a new type ImageList. This implements a TList to store all the images added. 2 functions are implemented to allow you to add a new image by supplying the filename and the image name and the get function will return a TImage given a image name.

The loadimages function still hardcodes the three images to load but you just need to read a text file to get the filename, image name values. I havent ever coded any file reading code so I thought Id leave that to you.

Hope you can follow it.

Im still having a problem figuring out why you need to name the images though. As you can see when I create an instance of the player and two types of enemies I still have to had code the image name that I want to reference.

Strict

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
	Field xMove
	Field yMove
	
	Method Draw()
		DrawImage(image,x,y)
	End Method
	
	Method Update()
		x = x + xMove
		y = y + yMove
	End Method
End Type

Type typePlayer Extends typeSprite
	Field health
	
	Method New()
		image = typeImageList.Get("The Hero")
	End Method
End Type

Type typeEnemy Extends typeSprite
	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]

Graphics 640,480,0,30	' Set the refresh rate to 30fps 

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","The 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
		player.Update()
		For enemy = EachIn enemyList
			enemy.Update()
		Next
		
		player.Draw()
		For enemy = EachIn enemyList
			enemy.Draw()
		Next
			
		Flip
	Wend
End Function