[Monkey] Null Object Access - Zwei Monkey Code Files [Solved

Übersicht Andere Programmiersprachen Beginners-Corner

Neue Antwort erstellen

 

SiriusMonk

Betreff: Null Object Access - Zwei Monkey Code Files [Solved]

BeitragSa, Jan 03, 2015 13:50
Antworten mit Zitat
Benutzer-Profile anzeigen
Question

Hi ,
ich bekomme ständig ein Null Object Access - Fehlermeldung - ich kann aber den Fehler nicht entdecken, vielleicht hat ja jemand die Güte mir dabei zu helfen . Der Code basiert auf dem Tutorials von
Smalltime Outlaws: Game-Tutorial 3
https://www.youtube.com/channe...iqu8Roujdw


Code:

Code: [AUSKLAPPEN]

        Method OnRender()
                Cls(0, 0, 0)
                Select gameState
                    Case STATE_MENU
                       DrawText("Falling Game", 320, 100, 0.5)
                       DrawText("Press Enter", 320, 400, 0.5)
                    Case STATE_GAME
                       PushMatrix()
                       Translate(came.position.x, came.position.y)
                       For Local block:= EachIn blocks
                           block.Draw()
                       Next
                       player1.Draw()
                       PopMatrix()
                     Case STATE_DEATH
                End Select
        End

In dieser Methode haut der Compiler die Fehlermeldung raus.

PushMatrix()
Translate(came.position.x, came.position.y)

und ich bekomm leider nicht raus warum Sad

HTML 5
MonkeyPro V81b.
JungelIDE 14.09.17a


Ich hoffe mir kann da jemand weiterhelfen.
Vielen Dank und Frohes Neues
Gruss SiríusMonk


main.monkey


Code: [AUSKLAPPEN]

Import Falling

Const STATE_MENU:Int = 0
Const STATE_GAME:Int = 1
Const STATE_DEATH:Int = 2

Global SCREEN_WIDTH:Int = 640
Global SCREEN_HEIGHT:Int = 400


Global TILE_W:Int = 48
Global TILE_H:Int = 48



Function Main:int()
   New FallingGame()
   Return 0
End

Class FallingGame Extends App
       
        Field gameState:Int = STATE_MENU
   
   Field gravity:Float = 0.2
   
   Field player1:Player = New Player(KEY_LEFT, KEY_RIGHT, 320, 50)
   
   Field came:Camera = New Camera()
   
   Field MapWidth:Int
   Field MapOffSet:Float
   Field blocks:List<Block> = New List<Block>()
   
   Method OnCreate()
   
     SetUpdateRate(60)
     MapWidth = SCREEN_WIDTH / TILE_W
     MapOffSet = (SCREEN_WIDTH Mod TILE_W / 2)
     GenerateFloor(3)
   End

   Method OnUpdate()
      Select gameState
        Case STATE_MENU
            If KeyHit(KEY_ENTER)
             gameState = STATE_GAME
          EndIf
      
      Case STATE_GAME
          If KeyHit(KEY_R) ' Reset Level
            player1.Reset()
            came.Reset() ' Cam Reset#
          EndIf
          player1.Update(gravity)
          came.Update(1.0)
         
   '   Case STATE_DEATH
         
      End Select
      
   End
   
   Method OnRender()
      Cls(0, 0, 0)
      Select gameState
         Case STATE_MENU
            DrawText("Falling Game", 320, 100, 0.5)
            DrawText("Press Enter", 320, 400, 0.5)
         Case STATE_GAME
                 PushMatrix()
                           Translate(came.position.x, came.position.y)
            For Local block:Block = EachIn blocks
               block.Draw()
            Next
                           player1.Draw()
                           PopMatrix()
   '      Case STATE_DEATH
         
      End Select
      
      
   End

   Method GenerateFloor(row:Int)
      For Local x:= 0 Until MapWidth
         blocks.AddLast(New Block(MapOffSet + TILE_W / 2 + x * TILE_W, row * TILE_H + TILE_H / 2))
      Next
   End
End


falling.monkey

Code: [AUSKLAPPEN]

Import mojo

Global TILE_W:Int = 48
Global TILE_H:Int = 48

Class Vec2D
   Field x:Float
   Field y:Float
   
   Method New(x:Float = 0, y:Float = 0)
      Set(x, y)
   End
   Method Set(x:Float, y:Float)
      Self.x = x
      Self.y = x
   End
End

Class Vec2Di
   Field x:Int
   Field y:Int
   Method New(x:Int = 0, y:Int = 0)
      Set(x, y)
   End
   Method Set(x:Int, y:Int)
      Self.x = x
      Self.y = x
   End
End




Class Camera
   Field originalPos:Vec2D
   Field position:Vec2D
   Method Reset()
      position.Set(originalPos.x, originalPos.y)
   End
   Method Update(fallSpeed:Float)
      position.y -= fallSpeed
   End
End

Class Player
   Field originalPos:Vec2D
   Field position:Vec2D
   Field velocity:Vec2D
   Field speed:Float = 4.0
   Field leftKey:Int
   Field rightKey:Int
   
   Method New(leftKey:Int, rightKey:Int, x:Float, y:Float)
      originalPos = New Vec2D(x, y)
      position = New Vec2D(x, y)
      velocity = New Vec2D()
      
      Self.leftKey = leftKey
      Self.rightKey = rightKey
   End
   
   Method Reset()
      position.Set(originalPos.x, originalPos.y)
      velocity.Set(0, 0)
   End
   
   Method Update(gravity:Float)
      velocity.x = 0
      velocity.y += gravity
   
                If KeyDown(leftKey)
           velocity.x = -speed
      EndIf
            If KeyDown(rightKey)
                velocity.x = speed
      EndIf
      position.x += velocity.x
                position.y += velocity.y
        End
   Method Draw()
      SetColor(0, 255, 0)
      DrawRect(position.x - 16, position.y - 16, 32, 32)
   End
End


Class Block
   Field position:Vec2D
     
   Method New(x:Float, y:Float)
      position = New Vec2D(x, y)
   End
   Method Draw()
                TILE_W = 48
      TILE_H = 48
      SetColor(123, 123, 123)
      DrawRect(position.x - (TILE_W / 2), position.y - (TILE_H / 2), TILE_W, TILE_H)
      SetColor(60, 60, 60)
      DrawRect(position.x - (TILE_W - 8 / 2), position.y - (TILE_H - 8 / 2), TILE_W - 8, TILE_H - 8)
   End
End





P.S. Sorrry für die Codedeformation Sad
  • Zuletzt bearbeitet von SiriusMonk am Sa, Jan 03, 2015 17:41, insgesamt einmal bearbeitet
 

PhillipK

BeitragSa, Jan 03, 2015 14:42
Antworten mit Zitat
Benutzer-Profile anzeigen
Ohne groß nachzuschauen:

BlitzMax: [AUSKLAPPEN]
Class Camera 
Field originalPos:Vec2D
Field position:Vec2D
Method Reset()
position.Set(originalPos.x, originalPos.y)
End
Method Update(fallSpeed:Float)
position.y -= fallSpeed
End
End


"position" wird niemals deklariert.
Nutze am besten eine New() methode, wo du die objekte (!!) ersteinmal erschaffst, bevor du sie benutzt (wie in deinem fehler - du greifst auf "came.position" zu, welches - soweit ichs sehe - nie erstellt wird. Ergo, null object access)

Des weiteren habe ich ehrlich gesagt nie getestet, ob man ein field direkt mit einer funktion / methode aufrufen kann (Field came:Camera = New Camera() ). Falls oben genanntes nicht klappt, schau, das du Fields erst in den OnCreate() etc methoden belegst. Aaaaber theoretisch klappt das auch so.


ps: Ich hab bewusst BM-syntax highlight verwendet. Liest sich hier im forum auch ganz gut Smile Lediglich "Class" wird nicht korrekt erkannt (in BM ist es "Type")
 

SiriusMonk

BeitragSa, Jan 03, 2015 17:40
Antworten mit Zitat
Benutzer-Profile anzeigen
Erstmal vielen Dank für die hyperschnelle Antwort Smile

Hab mir quasie ein New Operator geschrieben. Manchmal sieht man den Baum vor lauter Wäldern nicht Wink

Gruss SiriusMonk




main.monkey

BlitzMax: [AUSKLAPPEN]

Import Falling

Const STATE_MENU:Int = 0
Const STATE_GAME:Int = 1
Const STATE_DEATH:Int = 2



Function Main:Int()
New FallingGame()
Return 0
End

Class FallingGame Extends App
Field gameState:Int = STATE_MENU
Field gravity:Float = 0.2
Field player1:Player = New Player(KEY_LEFT, KEY_RIGHT, 100, 50)
Field came:Camera = New Camera(100, 50)
Field MapWidth:Int
Field MapOffSet:Float
Field blocks:List<Block> = New List<Block>()

Method OnCreate()
SetUpdateRate(60)
MapWidth = SCREEN_WIDTH / TILE_W
MapOffSet = (SCREEN_WIDTH Mod TILE_W) / 2
GenerateFloor(3)
End


Method OnUpdate()
Select gameState
Case STATE_MENU
If KeyHit(KEY_ENTER)
gameState = STATE_GAME
EndIf

Case STATE_GAME
If KeyHit(KEY_R) ' Reset Level
player1.Reset()
came.Reset() ' Cam Reset#
EndIf
player1.Update(gravity)
came.Update(0.0)

' Case STATE_DEATH

End Select

End

Method OnRender()
Cls(0, 0, 0)
Select gameState
Case STATE_MENU
DrawText("Falling Game", 320, 100, 0.5)
DrawText("Press Enter", 320, 400, 0.5)
Case STATE_GAME
PushMatrix()
Translate(came.position.x, came.position.y)
For Local block:Block = EachIn blocks
block.Draw()
Next
player1.Draw()
PopMatrix()

' Case STATE_DEATH

End Select
End

Method GenerateFloor(row:Int)
For Local x:= 0 Until MapWidth
blocks.AddLast(New Block(MapOffSet + TILE_W / 2 + x * TILE_W, row * TILE_H + TILE_H / 2))
Next
End

End


falling.monkey


BlitzMax: [AUSKLAPPEN]



Import mojo


Const SCREEN_WIDTH:Int = 640
Const SCREEN_HEIGHT:Int = 400


Const TILE_W:Int = 48
Const TILE_H:Int = 48

Class Vec2D
Field x:Float
Field y:Float

Method New(x:Float = 0, y:Float = 0)
Set(x, y)
End
Method Set(x:Float, y:Float)
Self.x = x
Self.y = x
End
End

Class Vec2Di
Field x:Int
Field y:Int

Method New(x:Int = 0, y:Int = 0)
Set(x, y)
End
Method Set(x:Int, y:Int)
Self.x = x
Self.y = x
End
End

Class Camera
Field originalPos:Vec2D
Field position:Vec2D

Method New(x:Float, y:Float)
originalPos = New Vec2D(x, y)
position = New Vec2D(x, y)
position.x = x
position.y = y
End

Method Reset()
position.Set(originalPos.x, originalPos.y)
End
Method Update(fallSpeed:Float)
position.y -= fallSpeed
End
End

Class Player
Field originalPos:Vec2D
Field position:Vec2D
Field velocity:Vec2D
Field speed:Float = 4.0
Field leftKey:Int
Field rightKey:Int

Method New(leftKey:Int, rightKey:Int, x:Float, y:Float)
originalPos = New Vec2D(x, y)
position = New Vec2D(x, y)
velocity = New Vec2D()
Self.leftKey = leftKey
Self.rightKey = rightKey
End

Method Reset()
position.Set(originalPos.x, originalPos.y)
velocity.Set(0, 0)

End

Method Update(gravity:Float)
velocity.x = 0
velocity.y += gravity

If KeyDown(leftKey)
velocity.x = -speed

EndIf
If KeyDown(rightKey)
velocity.x = speed

EndIf

position.x += velocity.x
position.y += velocity.y
End
Method Draw()
SetColor(0, 255, 0)
DrawRect(position.x - 16, position.y - 16, 32, 32)
End
End


Class Block
Field position:Vec2D
Method New(x:Float, y:Float)
position = New Vec2D(x, y)
Self.position.x = x
Self.position.y = y
End
Method Draw()
SetColor(123, 123, 123)
DrawRect(position.x - (TILE_W / 2), position.y - (TILE_H / 2), TILE_W, TILE_H)
SetColor(60, 60, 60)
DrawRect(position.x -((TILE_W-8)/2), position.y-((TILE_H-8)/2), TILE_W-8, TILE_H-8)
End
End

Neue Antwort erstellen


Übersicht Andere Programmiersprachen Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group