Can someone explain this to me?

BlitzPlus Forums/BlitzPlus Beginners Area/Can someone explain this to me?

ChrisM28(Posted 2015) [#1]
Im getting an error, it says 'custom type name, not found'

ive tried switching things around, renaming variables and such, but i keep getting this error.

;setup graphics
Graphics 640,480,32,2

Type redball
	Field x#
	Field y#
	Field mx#
	Field my#
End Type

Type blueball
	Field x#
	Field y#
	Field mx#
	Field my#
End Type

Type pinkball
	Field x#
	Field y#
	Field mx#
	Field my#
End Type

Type purpleball
	Field x#
	Field y#
	Field mx#
	Field my#
End Type

Type orangeball
	Field x#
	Field y#
	Field mx#
	Field my#
End Type

Type greyball
	Field x#
	Field y#
	Field mx#
	Field my#
End Type

Type greenball
	Field x#
	Field y#
	Field mx#
	Field my#
End Type

Type yellowball
	Field x#
	Field y#
	Field mx#
	Field my#
End Type


;Load Image Of Ball
img_redball    = LoadImage("C:\Users\Admin\Documents\Sprites\balls\redball.jpg")
img_blueball   = LoadImage("C:\Users\Admin\Documents\Sprites\balls\blueball.jpg")
img_pinkball   = LoadImage("C:\Users\Admin\Documents\Sprites\balls\pinkball.jpg")
img_purpleball = LoadImage("C:\Users\Admin\Documents\Sprites\balls\purpleball.jpg")
img_orangeball = LoadImage("C:\Users\Admin\Documents\Sprites\balls\orangeball.jpg")
img_greyball   = LoadImage("C:\Users\Admin\Documents\Sprites\balls\greyball.jpg")
img_greenball  = LoadImage("C:\Users\Admin\Documents\Sprites\balls\greenball.jpg")
img_yellowball = LoadImage("C:\Users\Admin\Documents\Sprites\balls\yellowball.jpg")

MidHandle img_redball
MidHandle img_blueball
MidHandle img_pinkball
MidHandle img_purpleball
MidHandle img_orangeball
MidHandle img_greyball
MidHandle img_greenball
MidHandle img_yellowball

;get radious of ball
radius = ImageHeight(img_redball)/2
radius = ImageHeight(img_blueball)/2
radius = ImageHeight(img_pinkball)/2
radius = ImageHeight(img_purpleball)/2
radius = ImageHeight(img_orangeball)/2
radius = ImageHeight(img_greyball)/2
radius = ImageHeight(img_greenball)/2
radius = ImageHeight(img_yellowball)/2

;generate balls
For x = 1 To 20
	balls.redball = New ball
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
	Next 
	
;generate balls
For x = 1 To 20
	balls.blueball = New ball
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
	Next 

;generate balls
For x = 1 To 20
	balls.pinkball = New ball
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
	Next 

;generate balls
For x = 1 To 20
	balls.purpleball = New ball
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
	Next 

;generate balls
For x = 1 To 20
	balls.orangeball = New ball
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
	Next 

;generate balls
For x = 1 To 20
	balls.greyball = New ball
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
	Next 

;generate balls
For x = 1 To 20
	balls.greenball = New ball
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
	Next 

;generate balls
For x = 1 To 20
	balls.yellowball = New ball
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
	Next 


;ballx# = 50
;bally# = 100
;my# = 0
;mx# = 2
ground = 450
gravity# = 0.4
timer = CreateTimer(30)

SetBuffer BackBuffer() 
While Not KeyHit(1)
Cls

;Update ball
my = my + gravity
bally = bally + my
ballx = ballx + mx
If bally > ground-radius Then bally = ground-radius : my = -my * 0.8 : mx = mx * 0.9
DrawImage img_redball,ballx,bally
DrawImage img_blueball,ballx,bally
DrawImage img_pinkball,ballx,bally
DrawImage img_purpleball,ballx,bally
DrawImage img_orangeball,ballx,bally
DrawImage img_greyball,ballx,bally
DrawImage img_greenball,ballx,bally
DrawImage img_yellowball,ballx,bally

WaitTimer timer
Flip
Wend
End 



Midimaster(Posted 2015) [#2]
with the line...
	balls.redball = New ball

you try to create a user variable from the type "ball". but you did nowhere define this type "ball".


Also the first lines look as you are not very familiar with user types. Why do you create different types with same properties? Wouldn't it be enough to create one type for all balls?
Type ball
        Field Image%
        Field Radius%
        Field color%
	Field x#
	Field y#
	Field mx#
	Field my#
End Type
....
RED%=1
GREEN%=2
....
;red balls
For x = 1 To 20
	balls.ball = New ball
        balls\Color=RED
        balls\Image=img_redball
        balls\Radius=ImageHeight(img_redball)/2
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
Next 




At the end you can call them all with:
For locBalls.ball =Each ball
     locBalls\X = locBalls\X + locBalls\mx
     locBalls\Y = locBalls\Y + locBalls\my
     If locBalls\Y > ground-radius Then locBalls\y = ground-radius 
          locBalls\my = -locBalls\my * 0.8
          locBalls\mx = locBalls\mx * 0.9
     Endif
     DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next



ChrisM28(Posted 2015) [#3]
Mr MidiMaster, i have modified the source code with the code you have suggested. But it still dont compile and run, so. What am i doing wrong?

;setup graphics
Graphics 640,480,32,2

Type ball
	Field Image%
	Field Radius%
	Field Color%
	Field x#
	Field y#
	Field mx#
	Field my#
End Type

RED%=1
BLUE%=2
PINK%=3
PURPLE%=4
ORANGE%=5
GREY%=6
GREEN%=7
YELLOW%=8

;red balls
For x = 1 To 20
	balls.ball = New ball
	balls\Color=RED
	balls\Image=img_redball
	balls\Radius=ImageHeight(img_redball)/2
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
Next 

For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius 
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next

;blue balls
For x = 1 To 20
	balls.ball = New ball
	balls\Color=BLUE
	balls\Image=img_blueball
	balls\Radius=ImageHeight(img_blueball)/2
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
Next

For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius 
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next

;pink balls
For x = 1 To 20
	balls.ball = New ball
	balls\Color=PINK
	balls\Image=img_pinkball
	balls\Radius=ImageHeight(img_pinkball)/2
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
Next

For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius 
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next

;purple balls
For x = 1 To 20
	balls.ball = New ball
	balls\Color=PURPLE
	balls\Image=img_purpleball
	balls\Radius=ImageHeight(img_purpleball)/2
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
Next

For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius 
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next

;orange balls
For x = 1 To 20
	balls.ball = New ball
	balls\Color=ORANGE
	balls\Image=img_orangeball
	balls\Radius=ImageHeight(img_orangeball)/2
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
Next

For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius 
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next

;grey balls
For x = 1 To 20
	balls.ball = New ball
	balls\Color=GREY
	balls\Image=img_greyball
	balls\Radius=ImageHeight(img_greyball)/2
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
Next

For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius 
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next

For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius 
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next

;green balls
For x = 1 To 20
	balls.ball = New ball
	balls\Color=GREEN
	balls\Image=img_greenball
	balls\Radius=ImageHeight(img_greenball)/2
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
Next

For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius 
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next

;yellow balls
For x = 1 To 20
	balls.ball = New ball
	balls\Color=YELLOW
	balls\Image=img_yellowball
	balls\Radius=ImageHeight(img_yellowball)/2
	balls\x = Rand (50)
	balls\y = Rand (100)
	balls\my = 0
	balls\mx = Rnd(0,1)*3
Next

For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius 
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9
EndIf

DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next

img_redball    = LoadImage("C:\Users\Admin\Documents\Sprites\balls\redball.jpg")
img_blueball   = LoadImage("C:\Users\Admin\Documents\Sprites\balls\blueball.jpg")
img_pinkball   = LoadImage("C:\Users\Admin\Documents\Sprites\balls\pinkball.jpg")
img_purpleball = LoadImage("C:\Users\Admin\Documents\Sprites\balls\purpleball.jpg")
img_orangeball = LoadImage("C:\Users\Admin\Documents\Sprites\balls\orangeball.jpg")
img_greyball   = LoadImage("C:\Users\Admin\Documents\Sprites\balls\greyball.jpg")
img_greenball  = LoadImage("C:\Users\Admin\Documents\Sprites\balls\greenball.jpg")
img_yellowball = LoadImage("C:\Users\Admin\Documents\Sprites\balls\yellowball.jpg")

MidHandle img_redball
MidHandle img_blueball
MidHandle img_pinkball
MidHandle img_purpleball
MidHandle img_orangeball
MidHandle img_greyball
MidHandle img_greenball
MidHandle img_yellowball

;ballx# = 50
;bally# = 100
;my# = 0
;mx# = 2
ground = 450
gravity# = 0.4
timer = CreateTimer(30)

SetBuffer BackBuffer() 
While Not KeyHit(1)
Cls

;Update ball
my = my + gravity
bally = bally + my
ballx = ballx + mx
If bally > ground-radius Then bally = ground-radius : my = -my * 0.8 : mx = mx * 0.9
DrawImage img_redball,ballx,bally
DrawImage img_blueball,ballx,bally
DrawImage img_pinkball,ballx,bally
DrawImage img_purpleball,ballx,bally
DrawImage img_orangeball,ballx,bally
DrawImage img_greyball,ballx,bally
DrawImage img_greenball,ballx,bally
DrawImage img_yellowball,ballx,bally

WaitTimer timer
Flip
Wend
End 



Midimaster(Posted 2015) [#4]
at first...
You are doing too much code, before testing it! In a code with hundred lines there may be dozends of bugs and interaction between them. If you start to code always test each new line with a program-start! again and again!


As a second advise....
You are starting with too complex programs. f.e. If you try to jump a lot of balls... why not start with a single one? Then, from day to day, add more features and code lines to your game.

I would suggest this steps:

1.day
=====
Create a User Type, load one image, then create a single ball with this type, finally try to display it.


2.day
=====
Try to move the ball over the screen


3.day
=====
.... and so on...


So from day to day your code grows and it always grows from a state, where it worked perfect yesterday. If now a problem appears, it must be in the work of today.

This could be a first day's work:
;setup graphics
Graphics 640,480,32,2

Type ball
	Field Image%
	Field x#
	Field y#
End Type

img_redball    = LoadImage("C:\Users\Admin\Documents\Sprites\balls\redball.jpg")


;red balls
For x = 1 To 20
	balls.ball = New ball
	balls\Image=img_redball
	balls\x = Rand (50)
	balls\y = Rand (100)
Next 

Repeat
	For locBalls.ball =Each ball
		DrawImage locBalls\Image, locBalls\X, locBalls\Y
	Next
	Flip
Until KeyHit(1)


difference to your code: image has to be loaded BEFORE balls are created!


ChrisM28(Posted 2015) [#5]
To Mr MidiMaster.

Thank you for showing me the correct build structure for the code. Following your example code given; i was able to make the modification i was aiming for.

Here's the result..

I made modifications to the 'Type ball' statement.

Type ball
	Field Image%
	Field Radius%
	Field Color%
	Field x#
	Field y#
	Field mx#
	Field my#	
End Type


By adding Field Radius%, Field Color%, Field mx%, and Field my%. The program compiled smoothly without consequence (Compilation Error) .

I Followed that up with adding the rest of the img_redball variables, and changing their name to their corresponding color images, and locations.

img_redball    = LoadImage("C:\Users\Admin\Documents\Sprites\balls\redball.jpg")

img_blueball   = LoadImage("C:\Users\Admin\Documents\Sprites\balls\blueball.jpg")

img_pinkball   = LoadImage("C:\Users\Admin\Documents\Sprites\balls\pinkball.jpg")

img_purpleball = LoadImage("C:\Users\Admin\Documents\Sprites\balls\purpleball.jpg")

img_greyball   = LoadImage("C:\Users\Admin\Documents\Sprites\balls\greyball.jpg")

img_greenball  = LoadImage("C:\Users\Admin\Documents\Sprites\balls\greenball.jpg")

img_yellowball = LoadImage("C:\Users\Admin\Documents\Sprites\balls\yellowball.jpg")


finishing up, i added in the needed code that are the dependencies for the above lines of code.

;red balls
For x = 1 To 20
	balls.ball = New ball
	balls\Image=img_redball
	balls\x = Rand (50)
	balls\y = Rand (100)
Next 

;blue balls
For x = 1 To 20
	balls.ball = New ball
	balls\Image=img_blueball
	balls\x = Rand (50)
	balls\y = Rand (100)
Next

;pink balls
For x = 1 To 20
	balls.ball = New ball
	balls\Image=img_pinkball		
	balls\x = Rand (50)
	balls\y = Rand (100)
Next

;purple balls
For x = 1 To 20
	balls.ball = New ball
	balls\Image=img_purpleball
	balls\x = Rand (50)
	balls\y = Rand (100)
Next

;grey balls
For x = 1 To 20
	balls.ball = New ball
	balls\Image=img_greyball
	balls\x = Rand (50)
	balls\y = Rand (100)
Next

;green balls
For x = 1 To 20
	balls.ball = New ball
	balls\Image=img_greenball
	balls\x = Rand (50)
	balls\y = Rand (100)
Next

;yellow balls
For x = 1 To 20
	balls.ball = New ball
	balls\Image=img_yellowball
	balls\x = Rand (50)
	balls\y = Rand (100)
Next


This is the block of code im having trouble understanding.

Repeat
	For locBalls.ball =Each ball
		DrawImage locBalls\Image, locBalls\X, locBalls\Y
	Next
	Flip
Until KeyHit(1)



ChrisM28(Posted 2015) [#6]
And now, im trying to figure out how to get the balls to move, like a bouncing ball would.


Midimaster(Posted 2015) [#7]
Repeat
    Cls
	For locBalls.ball =Each ball
		locBalls\X = locBalls\X + locBalls\mx
		DrawImage locBalls\Image, locBalls\X, locBalls\Y
	Next
    Flip
Until KeyHit(1)


well... the REPEAT...UNTIL is the games main loop. The first statement inside should always be a CLS, which clears the screen. The last statement in this block is always the FLIP command, which shows what was painted before. The KeyHit(1) quits the game when pressing the ESC key.

The interesting part is the FOR EACH loop. This is a possibility of stepping through all the elements of an type list. Each time you create a new element of the type BALL it will be added to the element list of this type. With FOR EACH you can call them all... and do whatever you want to do with them:

examples
==========

show values to the debugger console:
For locBalls.ball =Each ball
	Print locBalls\Color + "   " + locBalls\X + "     " + locBalls\Y
Next
WaitKey()



add something to the values:
For locBalls.ball =Each ball
	locBalls\X = locBalls\X + locBalls\mx
Next


find certain elements:
For locBalls.ball =Each ball
	If locBalls\Color = RED
		locBalls\Y = locBalls\Y + locBalls\my
	Endif
Next



dna(Posted 2015) [#8]
Hello.
In the Statement:
Type ball
	Field Image%
	Field Radius%
	Field Color%
	Field x#
	Field y#
	Field mx#
	Field my#	
End Type

How did you get the Color% variable to work. That's a reserved word in BB.


Midimaster(Posted 2015) [#9]
It works with Color% as variable. But you are right: it is better not to user reserved names.

So, ChrisM28, please replace Color% with MyColor%


Dan60(Posted 2015) [#10]
Wrong
For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next
Correct
For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then
locBalls\y = ground-radius
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next




When you have if and then on the same line you do not need to use endif


Dan60(Posted 2015) [#11]
Wrong
For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then locBalls\y = ground-radius
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next
Correct
For locBalls.ball =Each ball
locBalls\X = locBalls\X + locBalls\mx
locBalls\Y = locBalls\Y + locBalls\my

If locBalls\Y > ground-radius Then
locBalls\y = ground-radius
locBalls\my = -locBalls\my * 0.8
locBalls\mx = locBalls\mx * 0.9

EndIf
DrawImage locBalls\Image, locBalls\X, locBalls\Y
Next




When you have if and then on the same line you do not need to use endif


Dan60(Posted 2015) [#12]
And follow the advise of doing small steps and testing.