Call failed after creating class with javascript using Monaca

Asked 1 years ago, Updated 1 years ago, 42 views

I am a beginner at developing Monaca and JavaScript apps.
I'm sorry, but I'd like to ask you a question for your help.

I'm studying the rudiments at the following site, and I actually copy and run the same source code on Monaca, but somehow when I call the class, nothing appears on the screen.
http://code.9leap.net/

Source code for images as expected

 enchant(); // magic

// make one's own class Bear here
Bear=Class.create (successor of Sprite,//Sprite class)
{
    initialize:function(x,y){//initialize
        Sprite.call(this,32,32); // Initialize Sprite Object
        This.image=game.assets['./img/chara1.png'];
        This.x = x;
        This.y = y;

        this.tx = this.x; // X coordinates where you want to go
        this.ty = this.y;// Y coordinates where you want to go
        This.frame = 0;
        game.rootScene.addChild(this);
    },
    Define listeners for //enterframe events
    onenterframe:function(){
        slow = 30; // slowness of the bear's approach to the place it wants to go
        // Get close to where you want to go
        This.x+=(this.tx-this.x)/slow;
        This.y+=(this.ty-this.y)/slow;
    }
});

window.onload=function(){
    vargame = new Game (320, 320); // You are preparing the game itself and are configuring the amount of space displayed.
    game.fps = 24; // frames (frames) per second (seconds)—Sets the speed at which the game progresses.
    game.preload('./img/chara1.png');//pre(pre)-load(read): Preload the material to be used in the game.
    game.onload=function() {// Perform the main action when the game is ready.
        // bear = new Bear(100,120); // Make a bear  

        varkuma = new Sprite (32, 32); // You are preparing a sprite (operational image) and at the same time setting the size of the area the sprite will display.
        kuma.image=game.assets['./img/chara1.png']; // Apply preloaded images to the .
        Sets the horizontal position of the kuma.x=100;// bear.
        kuma.y = 120; // Sets the vertical position of the circle.
        game.rootScene.addChild(kuma);//Provides a dark spot in the game scene.
        game.rootScene.backgroundColor='#7ecef4';// Setting the background color of the game's operating parts.
    };
    game.start(); // Start the game! [Enter a description of the image here] [1]        
};

If you cancel the comment out below for class invocation, nothing will be displayed

 bear=new Bear(100,120); // Make a bear 

There are no errors on the console, and I don't know what the cause is, so if you notice anything, could you help me?

Thank you for your cooperation.

Enter a description of the image here

javascript monaca

2022-09-30 17:17

1 Answers

Perhaps the local variable game declared in window.onload should be a global variable.
Otherwise, the variable game cannot be referenced from the initialize of the Bear class.
I think you just need to declare the variable game outside.

var game;
window.onload=function(){
    game = new Game(300,300);
// The rest are the same.


2022-09-30 17:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.