Multiple entities

przemek

Member
Joined
Sep 18, 2017
Hi

In my code I have put a movable character and I want to input another entity. However when i put code to draw it, it says memory access violation. Can someone help me a bit with that? (sorry for being a bit bothersome)
Code:
Strict

Import mojo




Class Game Extends App
       
              
      
      
'timer
    Field lastTick:Int
    Field secs:Int
    Field mins:Int
    Field hrs:Int
    Field dys:Int
 
  'resources
    Field wood:Float
     Field loopedtimeforwood:Int
    
    Field x2:Float = 500
    Field y2:Float = 400
    
    
    
'character
    Field x:Float = 575
    Field y:Float = 420
    Field size:Float = 10
    Field BorderXMin:Float = 1
    Field BorderXMax:Float = 1270
    Field BorderYMin:Float = 25
    Field BorderYMax:Float = 870
   
     Field character:Image
     Field Farm:Image
     Field p1:Game
    

   
   
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    Method OnCreate:Int()
        SetUpdateRate(0)
        lastTick = Millisecs()
        loopedtimeforwood = 0
        wood = 0
       
        SetDeviceWindow(1280,960,0)
       
   
       
        'character
        character = LoadImage("character.png")
        Farm = LoadImage("farm.png")
        SetUpdateRate 60
       
       
       
        Return 0
       
    End Method
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
    Method OnUpdate:Int()
        Local tick:Int = Millisecs()
        If tick>=(lastTick+1000)
            lastTick = tick
            secs += 1
            loopedtimeforwood+=1
        Endif
        If secs > 59 Then
            secs = 0
            mins +=1
            If mins > 59 Then
                mins = 0
                hrs +=1
                If hrs > 23 Then
                    hrs = 0
                    dys += 1
                Endif
            Endif
        Endif
       
       If loopedtimeforwood >= 20
               wood+=1
               If KeyDown (KEY_A) Then wood+=1
           loopedtimeforwood =0
        End
       
       x = Clamp(x, BorderXMin, BorderXMax - size)
       y = Clamp(y, BorderYMin, BorderYMax - size)
       
       
       If KeyDown (KEY_LEFT) Then x = x - 4
       If KeyDown (KEY_RIGHT) Then x = x + 4
       If KeyDown (KEY_UP) Then y = y-4
       If KeyDown (KEY_DOWN) Then y = y +4
       'keydown means keep moving until key is down where keyhit is do a thing until
      
     
      
        Return 0
    End Method
    Method OnRender:Int()
       
        Cls 124, 256, 0
        DrawText("Mins: "+secs, 10, 12)
        DrawText("Min: "+mins, 80, 12)
        DrawText("Hour: "+hrs, 140, 12)
        DrawText("Day: "+dys, 200, 12)''''''''''''''''
        DrawText("Wood: "+wood, 400, 80)
       
       
       
       
       
        DrawImage character, x, y
        DrawImage Farm, x2, y2
        Return 0
       
    End Method
   

   
End Class



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

przemek

Member
Joined
Sep 18, 2017
I guess I'll need to have some sort of function for each of them but dont know how to set them not to get an error too.
 

Phil7

Moderator
Joined
Jun 26, 2017
Here it works fine. Make sure, you are spelling the image files correctly. Farm.png or farm.png can make the difference.
I used these images for testing:

character.png
farm.png
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
Yes, the code looks fine, the image probably failed to load because of mis-spelling or something.
 

Phil7

Moderator
Joined
Jun 26, 2017
Yeah, don't feel sorry! In programming you have to get used to making stupid mistakes one after the other
and still keep doing it. ;-)
 
Top Bottom