Jump to content

Trouble with collision in enchant.js


Recommended Posts

It's a long shot but I'm giving this a shot. I'm dealing with enchant.js and I get the grasp of it but not entirely.

My biggest problem is having my function shootingPokeball and function makePokeball in SCENE 2 to collide with one anothe

I'm programming through a website: 9leap.net

 

// --- Guide ---

// Stage 1:
// Avoid the POKEBALLS to remain alive for 15 seconds. Grab as many RARE CANDY for points (20).

// Stage 2:
// Shoot down as many POKEBALLS to collect points (10 each).






window.onload = myGame;
enchant();


// GAME

function myGame() {

        var game = new Core(600, 600);
        game.fps = 24;
        
        // PRELOADS
        game.preload('pokeball.png', 'shock.png', 'rarecandy.png', 'explosion.png', 'magneton.png',
          'space.png', 'space2.png');
        game.preload ('warsong.mp3', 'capturedEnd.mp3', 'ff4.mp3', 'quick.mp3', 'punch.mp3');
        
        game.score = 0;
        
        
        
        game.onload = function () {
        
     
        
        
        game.deck = new Sprite(49, 48);
        game.deck.image = game.assets['magneton.png'];
        game.deck.x = (game.width - game.deck.width) / 2;
        game.deck.y = (game.height - game.deck.height) / 2;
        game.rootScene.addChild(game.deck);
        game.bgm = game.assets['quick.mp3']; // Intro sound
        game.bgm.play();
        game.screenMsg = new Label("Dodge the incoming dangers <br>until Stage 2. <br>When Stage 2 begins, <br>shoot down as many dangers as you can.");
        game.screenMsg.color = 'gold';
        game.rootScene.addChild(game.screenMsg);
        
        
        game.deck.addEventListener(Event.TOUCH_END, function (event) {
            game.pushScene(stage1());
        });
    };
    
   
    game.start();





    //--------------
    //-----SCENE 2--
    //--------------
    
    function stage2() {
        var scene = new Scene();
        
        var pokeballArray [qw, ];
        
        // BG FOR STAGE 2
        var bg = new Sprite(game.width, game.height);
        bg.image = game.assets['space2.png'];
        scene.addChild(bg);
        
        
        scene.timerDown = {
            frameCount: 10 * game.fps,
            tick: function () {
                if (game.isStarted) {
                    this.frameCount -= 1;
                }
            }
        };
        
        // TIME
        var timeLabel = new Label("");
        scene.addChild(timeLabel);
        timeLabel.color = 'gold';
        
        // MAIN HERO
        var char = heroChar(scene);
        scene.addChild(char);
        
        
        
         // Rebind input.a key to spacebar
        game.keybind(32, 'a');     // 32 is the ASCII code for a space.
        
        
        // Add an event listener for each frame.
        game.addEventListener(Event.ENTER_FRAME, function () {
            // fire a shock blast in the spacebar is down, but only
            // every 3rd frame
            if (game.input.a && game.frame % 3 === 0) {
                shootingPokeball(char);
            }
        });
        
        scene.addEventListener(Event.ENTER_FRAME, function () {
            if (scene.timerDown.frameCount > 0) {
                
                
                if (game.frame % 24 === 0) {
                    makePokeball(char, scene, pokeballArray);
                }
                
              
                scene.timerDown.tick();
                
                
                
                timeLabel.text = 'Time: ' +
                    Math.ceil(scene.timerDown.frameCount / game.fps);
            } else if (scene.timerDown.frameCount !== -1) {
                scene.timerDown.frameCount = -1;
                game.screenMsg.text = 'Congratualations!<br>Score: ' + game.score;
                game.popScene();
                game.bgm.stop();
                game.bgm = game.assets['ff4.mp3'];
                game.bgm.play();
                game.end();
            }
        });
        return scene;
    }






    // VILLIAN FOR STAGE 2

    function makePokeball(char, evil) {
        var pokeball = new Sprite(48, 48);
        pokeball.image = game.assets['pokeball.png'];
        
        var magneton = heroChar();
           
        
        // pokeball SPEED FROM TOP STAGE 2
        pokeball.gravity = 10;
        
        // RANDOM pokeball SPAWNING POINT
        pokeball.x = randomInt(0, game.width - pokeball.width);
        pokeball.y = randomInt(-1 * game.height, -1 * pokeball.height);
        
        evil.addChild(pokeball);
        
        // EVENT LISTENERS
        pokeball.addEventListener(Event.ENTER_FRAME, function () {
            
            
            
            game.isStarted = true;
            
            
            if (pokeball.y > game.height) {
                evil.removeChild(pokeball);
            } else {
                pokeball.y += pokeball.gravity;
            }
            
            // COLLISONS FOR STAGE 2
            if (pokeball.intersect(char)) {
                evil.removeChild(pokeball);
                shootingPokeball();
                
                scoringScene(villain, true);
            }
        });
        
        pokeball.addEventListener(Event.ENTER_FRAME, function (event) {
            if (pokeball.scaleX > 1.0) {
                pokeball.isGrowing = false;
            } else if (pokeball.scaleX < 0.8) {
                pokeball.isGrowing = true;
            }
            if (pokeball.isGrowing) {
                pokeball.scaleX += 0.5;
            } else {
                pokeball.scaleX -= 0.035;
            }
            pokeball.scaleY = pokeball.scaleX;
        });
        game.punchSound = game.assets['punch.mp3'];
        
        return pokeball;
    }





      
    // SHOOTING FOR STAGE 2
    
      function shootingPokeball(char, pokeball, pokeballArray) {
        var thunder = new Sprite(15, 18);
        thunder.image = game.assets['shock.png'];
        
        // Add a speed property to the thunder.
        thunder.speed = 10;  // How fast will thunder move?

        // Position the thunder blast over char's eyes
        thunder.x = char.x + 35;
        thunder.y = char.y + -25;
        
        // Add thunder to scene.
        game.currentScene.addChild(thunder);

        // Event listeners
        thunder.addEventListener(Event.ENTER_FRAME, function () {
          
            // MOVE UP TOWARD CANVAS
            this.y -= this.speed;
            
            // EXPLOSION
            
            if (this.intersect(pokeball)) {
                pokeball.image = game.assets['explosion.png'];
                game.currentScene.removeChild(this);
            }
            
            // Delete if off screen.
            if (this.y < 0 - this.height) {
                game.rootScene.removeChild(this);
                // this = null;
            }
        });
        
        return thunder;
    }
    
    


     // HERO FOR STAGE 2
     
     function heroChar() {
        var magneton = new Sprite(49, 48);
        magneton.image = game.assets['magneton.png'];

        // Add a speed property to the magneton.
        magneton.speed = 10;  // How fast will magneton move?

        magneton.x = (game.width + magneton.width) / 2;
        magneton.y = game.height - magneton.height;
        
        
        // Add magneton to scene.
        game.rootScene.addChild(magneton);

        // Event listeners for magneton.
        magneton.addEventListener(Event.ENTER_FRAME, function () {
          
            // Move.
            if (game.input.right && !game.input.left) {
                magneton.x += magneton.speed;
            } else if (game.input.left && !game.input.right) {
                magneton.x -= magneton.speed;
            }
            
            
            // Check limits.
            if (magneton.x > game.width - magneton.width) {
                magneton.x = game.width - magneton.width;
            } else if (magneton.x < 0) {
                magneton.x = 0;
            }
        });
        
        return magneton;
    }







    //-------------
    //---SCENE 1---
    //-------------
    
    function stage1() {
        var scene = new Scene();
        
        // BG FOR STAGE 1
        var bg = new Sprite(game.width, game.height);
        bg.image = game.assets['space.png'];
        scene.addChild(bg);
        
        
       
        
               // Sounds
        game.bgm = game.assets['warsong.mp3']; // background music
        game.bgm.play();
        
        scene.timerUp = {
            frameCount: 0,
            tick: function () {
                if (game.isStarted) {
                    this.frameCount += 1; // COUNTDOWN
                }
            }
        };

        
        var timeLabel = new Label("");
        scene.addChild(timeLabel);
        timeLabel.color = 'red';
        
        var char = safeChar(scene);
        var candy = collectRareCandy(scene);
        
        
        //  Event Listener
        scene.addEventListener(Event.ENTER_FRAME, function () {
          
          
            if (game.frame % 12 === 0) {
                dodgePokeball(char, scene);
            }

            
            scene.timerUp.tick();

            
            timeLabel.text = 'Time: ' +
                Math.floor(scene.timerUp.frameCount / game.fps);

            
            if (scene.timerUp.frameCount >= 5 * game.fps) {
                scoringScene(scene, false);
            }
        });

        return scene;
    }








    // VILLAIN FOR STAGE 1
    
    function dodgePokeball(char, villain) {
        var pokeball = new Sprite(48, 48);
        pokeball.image = game.assets['pokeball.png'];
        
        
        
        
        // pokeball SPEED.
        pokeball.gravity = 15;
        
        
        // Position pokeball in random x and random y above screen.
        pokeball.x = randomInt(0, game.width - pokeball.width);
        pokeball.y = randomInt(-1 * game.height, -1 * pokeball.height);
        
        
        villain.addChild(pokeball);
        
        
        // Add an event listener to the pokeball Sprite to move it.
        pokeball.addEventListener(Event.ENTER_FRAME, function () {
            // Game starts when first drop starts falling.
            game.isStarted = true;
            
            // Move.
            if (pokeball.y > game.height) { // if below canvas, remove.
                villain.removeChild(pokeball);
            } else { // else move down as usual.
                pokeball.y += pokeball.gravity;
            }
            
            // COLLISIONS
            if (pokeball.intersect(char)) {
                villain.removeChild(pokeball);
                
                scoringScene(villain, true);
            }
            
            game.punchSound = game.assets['punch.mp3'];
        });
        
        return pokeball;
    }
    
    
    
    
    
    
    
    
   // COLLECT FOR STAGE 1
   
    function collectRareCandy(rare, char, hp) {
        var candy = new Sprite(50, 49);
        candy.image = game.assets['rarecandy.png'];
        
        
        // candy SPEED.
        candy.gravity = 8;
        
        
        // Position candy in random x and random y above screen.
        candy.x = randomInt(0, game.width - candy.width);
        candy.y = randomInt(-1 * game.height, -1 * candy.height);
        
        
        rare.addChild(candy);
        
        
        // Add an event listener to the candy Sprite to move it.
        candy.addEventListener(Event.ENTER_FRAME, function () {
            
            game.isStarted = true;
            
            // Move.
            if (candy.y > game.height) { // if below canvas, remove.
                
            } else { // else move down as usual.
                candy.y += candy.gravity;
            }
            
            // COLLISIONS
            if (candy.intersect(char)) {
                hp.removeChild(candy);
                
                game.currentScene(hp, true);
            }
        });
        
        // CANDY SCALING
        candy.addEventListener(Event.ENTER_FRAME, function (event) {
            if (candy.scaleX > 1.0) {
                candy.isGrowing = false;
            } else if (candy.scaleX < 0.8) {
                candy.isGrowing = true;
            }
            if (candy.isGrowing) {
                candy.scaleX += 0.035;
            } else {
                candy.scaleX -= 0.035;
            }
            candy.scaleY = candy.scaleX;
        });
        
        return candy;
        
        
    }










    // HERO FOR STAGE 1
    function safeChar(main) {
        var char = new Sprite(49, 48);
        char.image = game.assets['magneton.png'];

        // SPEED IN STAGE 1
        char.speed = 13;

        char.x = (game.width + char.width) / 2;
        char.y = game.height - char.height;

        
        main.addChild(char);
        
        
       var candy = new Sprite(32, 23);
        candy.image = game.assets['rarecandy.png'];
        
        
        

        // EVENT LISTENERS
        char.addEventListener(Event.ENTER_FRAME, function () {
            
            if (game.input.right && !game.input.left) {
                char.x += char.speed;
            } else if (game.input.left && !game.input.right) {
                char.x -= char.speed;
            }

          
            if (char.x > game.width - char.width) {
                char.x = game.width - char.width;
            } else if (char.x < 0) {
                char.x = 0;
            }
        });

        return char;
    }

    
    // SCORE SCENE
    function scoringScene(scoring, gameOver) {
       
        game.score += Math.floor(scoring.timerUp.frameCount / game.fps);

        if (!gameOver) {
           
            game.score += 200;
            
            game.replaceScene(stage2());
            
            
        } else {

            // ENDING MESSAGE
            game.screenMsg.text = 'Hope you had fun! Better luck next time.<br>Score: ' + game.score;
            
            game.popScene();
            
            game.bgm.stop();
            game.bgm = game.assets['capturedEnd.mp3'];
            game.bgm.play();
            game.punchSound.play();
            
            game.end();
        }
    }

  

 

Edited by K31V
Link to post
Share on other sites

My biggest problem is having my function shootingPokeball and function makePokeball in SCENE 2 to collide with one anothe

What does that mean? How do 2 functions collide? What exactly happens? Are you checking for error messages in the console?
Link to post
Share on other sites

What does that mean? How do 2 functions collide? What exactly happens? Are you checking for error messages in the console?

What suppose to happen is:

When "thunder" collides with "pokeball," "pokeball suppose to be remove from the canvas

The sprites "thunder" and "pokeball" are not colliding.

 if (this.intersect(pokeball)) {
                pokeball.image = game.assets['explosion.png'];
                game.currentScene.removeChild(this);

There's no error on the console between the 2 sprites.

 

Link to post
Share on other sites

Are you saying that if statement never evaluates to true, or maybe there's other code that causes it to get skipped completely?

 

You can use console.log statements to write information to the browser's console, use that to help you figure out what's going on. Write messages to tell you where the program is, what it's doing, if it's inside an if statement, etc. Write out the values of variables so that you can check the data to make sure everything is correct. You can also use breakpoints in the developer tools to step through the code one line at a time to give you a chance to see exactly what it's doing and what the values of all of the variables are before it executes the next line.

Link to post
Share on other sites
Hello ;


I wrote a browser game in Enchant.js , where I did some collisions .

It is contained in this .zip file :


The file you are probable most interested is this one :

BenghaziGameTest.js

I stopped using enchantjs because they apparently stopped developing it .


So I rewrote it in Phaser , here :


Didn't care for Phaser too much . It is so laaarge .


So I rewrote it in Quick.js , here :


Runs great , but the DOCs weren't very good .


Then I thought 'why keep fiddling with all these languages .

Since they are all based on javascript , why not just learn javascript .'

So I am writing it in javascript here :


It is almost complete . Just having trouble with the 'MoveRight' button .


The folks on this Forum are awesome !!!


Anyways , take a look at enchant.js code :



Happy Trails...


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...