Mojo2 load image

Ferret3D

New member
Joined
Aug 13, 2017
Messages
88
I cant get an image to load, what am i doing wrong?
I have a game.data folder.

Code:
Import mojo2

Class MyApp Extends App
    Field canvas:Canvas
    Field test:Image
   
    Method OnCreate()
        canvas = New Canvas
        SetUpdateRate 60
       
        test.Load("test.jpg")

    End
   
    Method OnUpdate()
   
    End
   
    Method OnRender()
   
        canvas.Clear 0,0,1
       
        canvas.DrawImage(test)
       
        canvas.Flush
       
    End
End

Function Main()
    New MyApp
End
[\code]
 
Is your script named game.cxs? They both have to be the same before the postfix.

game.cxs
game.data


Also on mobile platforms, the file names are case sensitive. So Test.jpg and test.jpg would be two different images. Make sure your file name inside the script is equal to the actual file name.
 
PhotoPuzzle.cxs
PhotoPuzzle.data
test.jpg

When i try to draw the image,
Cerberus Runtime Error : Null object access
 
Can you please zip the project and attach it here? Will have a look later.
 
What York says.
 
I cant get an image to load, what am i doing wrong?
You are basically calling a static class function that returns a Image object as a result and not assigning the result into a 'handle' variable of the data type expected, in this case Image.

Now if I remember when you declare an variable with Field in a class and create this class with New, all Fields are given a default value, unless you over-ride by assigning a value. This default value depends on the data type assigned, e.g. int = 0, float = 0.0, String = "" and Class objects such as Image are given the value of Null. So when you declared test as a field you basically created an empty data object of the Image class with an alias name of test with access to all those static functions ready for use, but it has no data.

Now as the Image class has a static function called Load; this is not a method. You should think of calls defined with Function as static or global to a class, these only operate at class level and not on the objects that you create with the New keyword; that's what you would use methods for.

The correct way is to assign the result as York has shown.
 
Last edited:
This one should be Sticky Topics because you dont want every New Beginner Cerberus Users repeating the same questions when posting new topics on the forum if they get stuck on how to display images on screen!
 
If the docs stayed the way they are, yes of course. But I really hope, we don't need these kind of threads once the new docs are in place.
 
Back
Top Bottom