• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Multiple entities

przemek

New member
Joined
Sep 18, 2017
Messages
62
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
 
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.
 
What's the line that causes the memory access violation?
 
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
 
Yes, the code looks fine, the image probably failed to load because of mis-spelling or something.
 
Happens to everyone. Was it just a typo?
 
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. ;-)
 
Back
Top Bottom