score not working
Blitz3D Forums/Blitz3D Programming/score not working
| ||
could someone what I need to do to get my score to work. Here is the code so far. I have it so that when an asteroid is deleted, the score (bam) should increment by 1. But obviously, this is not setup correctly. Any help is appreciated. Thanks. Graphics3D 800,600,32,1 SetBuffer BackBuffer() SeedRnd (MilliSecs()) camera=CreateCamera () point=CreatePivot() mainlight=CreateLight() LightColor mainlight,255,255,10 sublight=CreateLight() LightColor sublight,-100,-100,-100 indlight=CreateLight() LightColor indlight,255,2,255 PositionEntity camera,0,100,0 PositionEntity mainlight,20,5,20 PositionEntity sublight,0,5,-0 PositionEntity indlight,0,2,-100 PositionEntity point,0,0,0 PointEntity camera,point PointEntity mainlight,point PointEntity sublight,point PointEntity indlight,point Const rock_collision = 1 Const bullet_collision = 2 Const ship_collision=3 scorepoint=0 Type asteroid Field aster Field x# Field y# Field z# Field speedx# Field speedz# Field spin# Field life Field distance# End Type rock=LoadMesh("images/rock1a.3ds") EntityType rock,rock_collision HideEntity rock bam=0 For n=0 To 9 ast.asteroid=New asteroid ast\aster=CopyEntity(rock) ast\x#=Rnd(-50,50) ast\y#=0 ast\z#=Rnd(-50,50) ast\speedx#=Rnd(-.2,.2) ast\speedz#=Rnd(-.2,.2) ast\spin# = Rnd(-0.5,0.5) ast\life=5 PositionEntity ast\aster,ast\x#,0,ast\z# RotateEntity ast\aster,0,Rnd(0,359),0 ScaleEntity ast\aster,.5,.5,.5 ;mark=mark+1 Next player=LoadMesh("images/playership.3ds") EntityType player,ship_collision dist#=0.05 x#=0 y#=0 z#=0 Type bullet Field entity Field distance_to_travel# Field x# Field y# Field z# End Type ;-- create base bullet entity Global bullet_entity = LoadMesh("images/bullet.x");CreateSphere(6) ;use your sprite here EntityColor bullet_entity, 255, 0, 0 EntityType bullet_entity,bullet_collision ;set collision type HideEntity bullet_entity ;--setup sphere_to_polygon collision between rocks and bullets Collisions bullet_collision,rock_collision, 2, 1 ;MAIN LOOP------------------------------------ While Not KeyHit(1) asteroidism() MoveBullets() ;left and right turning--------------------------- If KeyDown( 203 )=True Then angle#=angle#+3.0 If KeyDown( 205 )=True Then angle#=angle#-3.0 ;thrust--------------------------------------- If KeyDown( 200 )=True Then x#=x#+Cos(angle#+90.0)*dist# z#=z#+Sin(angle#+90.0)*dist# EndIf ;brakes------------------------------------ If Not KeyDown(200) x#=0+(x#*0.995) z#=0+(z#*0.995) EndIf ;reposition on screen if off screen------------ If EntityZ#(player)>100 PositionEntity player,x#,y#,-100 EndIf If EntityX#(player)>120 PositionEntity player,-120,y#,z# EndIf If EntityZ#(player)<-100 PositionEntity player,x#,y#,100 EndIf If EntityX#(player)<-120 PositionEntity player,120,y#,z# EndIf ;movement of player----------------------------------------- RotateEntity player,0.0,angle#,0.0 TranslateEntity player,x#/2,0.0,z#/2 ;fire new bullet If KeyDown(157) ;change to MouseHit(1) for mouse button 1 timage=timage+1 If timage=1 shoot.bullet = New bullet shoot\entity = CopyEntity( bullet_entity, player ) ;<parent> parameter here is IMPORTANT EntityParent shoot\entity, 0 ;remove from parent so it can move on its own shoot\distance_to_travel = 50 EndIf ElseIf Not KeyDown(157) timage=0 EndIf If timage>10 Then timage=0 EndIf UpdateWorld RenderWorld Text 50,50,"SCORE: "+bam Flip Wend End Function asteroidism() For ast.asteroid=Each asteroid smack=EntityCollided(ast\aster,bullet_collision) If smack FreeEntity ast\aster Delete ast bam=bam+1 Else TurnEntity ast\aster,ast\spin#,ast\spin#,ast\spin# TranslateEntity ast\aster,ast\speedx#,0,ast\speedz# If EntityZ#(ast\aster)>100 PositionEntity ast\aster,ast\x#,ast\y#,-100 EndIf If EntityX#(ast\aster)>120 PositionEntity ast\aster,-120,ast\y#,ast\z# EndIf If EntityZ#(ast\aster)<-100 PositionEntity ast\aster,ast\x#,ast\y#,100 EndIf If EntityX#(ast\aster)<-120 PositionEntity ast\aster,120,ast\y#,ast\z# EndIf EndIf Next End Function Function MoveBullets() ;-- go through all active bullets... For shoot.bullet = Each bullet zap=EntityCollided(shoot\entity,rock_collision) If zap FreeEntity shoot\entity Delete shoot Else ;move bullet forward MoveEntity shoot\entity, 0,0,2 ;reduce counter of distance to travel until finished movement shoot\distance_to_travel = shoot\distance_to_travel - 1 ;remove bullet entity? (finished it's movement (or collided with block)) If shoot\distance_to_travel <= 0 ;remove entity and delete bullet type instance FreeEntity shoot\entity Delete shoot Else If EntityZ#(shoot\entity)>100 PositionEntity shoot\entity,shoot\x#,shoot\y#,-100 EndIf If EntityX#(shoot\entity)>120 PositionEntity shoot\entity,-120,shoot\y#,shoot\z# EndIf If EntityZ#(shoot\entity)<-100 PositionEntity shoot\entity,shoot\x#,shoot\y#,100 EndIf If EntityX#(shoot\entity)<-120 PositionEntity shoot\entity,120,shoot\y#,shoot\z# EndIf EndIf EndIf Next End Function |
| ||
You have set bam local to main program and local to function asteroidism. bam in your main program is out of scope for your functions. If you want to modify it from within a function, you have to either: Pass it to the function and return it from the function or Make it a global variable. Also, I would highly recommend indenting your code for readability reasons. |
| ||
Thanks Wolron, worked. Actually the code was indented. I guess when i cut and pasted, it whacked the indentation. sorry about that. Thanks again. |