Jump to content

Something bad with code.


yaroslav kornilov
 Share

Recommended Posts

Plese help me with this code, it work's good first 5-6 seconds and then crash(all sprites stop), i don't know to do with it:

 

 

//begin

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });
 
var MainHero;//ок
var Zombie;//ок
var upKey;//ок
var leftKey;//ок
var rightKey;//ок
var bullet;
var bullets;
var bulletTime = 0;
var backgroundimage;
 
function preload() {
    game.load.spritesheet('MainHero', 'playerwithpistol.png', 17, 9, 24); //главный герой 
    game.load.spritesheet('Zombie','zombies.png', 11, 15, 6); // враги
    game.load.image('bullet', 'bullet.png');  //пуля
    game.load.image('backgroundimage','backgroundimage.jpg');//фон
}
 
function create() {
    //главный герой
    MainHero = game.add.sprite(30, 400,'MainHero');  
    MainHero.animations.add('left', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 10, true);//идти влево
    MainHero.animations.add('right', [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], 10, true);//идти в право
    upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
    leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
    rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
    //пули
    bullets = game.add.group();
    bullets.createMultiple(10, 'bullet');
    bullets.callAll('events.onOutOfBounds.add', 'events.onOutOfBounds', resetBullet, this);
 
    //enemy
    Zombie = game.add.sprite(600,30,'Zombie');
    Zombie.animations.add('walk');
    Zombie.animations.play('walk', 20, true);
 }
 
function update() {
   MainHero.body.velocity.x = 0;
   MainHero.body.velocity.y = 0;
   //input getting
   if (leftKey.isDown)
    {
        MainHero.x--;
        MainHero.animations.play('left', 10, true);
 
    }
    else if (rightKey.isDown)
    {
        MainHero.x++;
        MainHero.animations.play('right', 10, true);
    }
 
    if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR))
    {
        fireBullet();
    }
    
     if (upKey.isDown && MainHero.body.touching.down)
    {
        MainHero.body.velocity.y = -350;
    }
 
}
 
//отдельный модуль 2
function resetBullet (bullet) {
    bullet.kill();
}
 
//отдельный модуль
function fireBullet () {
 
    if (game.time.now > bulletTime)
    {
        bullet = bullets.getFirstExists(false);
 
        if (bullet)
        {
            bullet.reset(MainHero.x + 3, MainHero.y - 8);
            bullet.body.velocity.x = +300;
            bulletTime = game.time.now + 250;
        }
    }
 
}
 
//end.
 
 
 
 
 
 
 
 
//
 //function create() {
 
 
    ///platforms = game.add.group();
    ///var ground = platforms.create(0, game.world.height - 64, 'ground');
    //ground.scale.setTo(2, 2);
    //ground.body.immovable = true;
    //var ledge = platforms.create(400, 400, 'ground');
    ///ledge.body.immovable = true;
 
    //ledge = platforms.create(-150, 250, 'ground');
    //player.body.gravity.y = 6;
 
//}
//отдельный конструктор 
 
//самое важное
//функция взаимодействия
 
 
 thank's
Link to comment
Share on other sites

All your code does is allow the player to move left right and shoot, and I just tested it using placeholder asset and it work fine. Not sure what's going on on your side. Note that I removed all animations in order to be able to test it without your assets. So if there is a problem it might be with the animations causing an error and making the game stop.

Link to comment
Share on other sites

body.scrollTop is deprecated in strict mode. Please use 'documentElement.scrollTop' if in strict mode and 'body.scrollTop' only if in quirks mode. phaser.js:20550
body.scrollLeft is deprecated in strict mode. Please use 'documentElement.scrollLeft' if in strict mode and 'body.scrollLeft' only if in quirks mode. phaser.js:20551
Phaser v1.1.3 - Renderer: WebGL - Audio: WebAudio phaser.js:13613
  1. Uncaught TypeError: Cannot read property 'uuid' of null phaser.js:27995
    1. Phaser.Animation.updatephaser.js:27995
    2. Phaser.AnimationManager.updatephaser.js:27547
    3. Phaser.Sprite.updateAnimationphaser.js:18657
    4. Phaser.Sprite.preUpdatephaser.js:18586
    5. Phaser.World.updatephaser.js:13066
    6. Phaser.Game.updatephaser.js:13699
    7. Phaser.RequestAnimationFrame.updateRAFphaser.js:22114
    8. _onLoop

This i get with crome- dev tools, i looked into the phaser source, but i can't find whe my code crash. The problem is with animations.

Link to comment
Share on other sites

//beginvar game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update }); var MainHero;//окvar Zombie;//окvar upKey;//окvar leftKey;//окvar rightKey;//окvar bullet;var bullets;var bulletTime = 0;var backgroundimage; function preload() {    game.load.spritesheet('MainHero', 'playerwithpistol.png', 17, 9, 24); //главный герой     game.load.spritesheet('Zombie','zombies.png', 11, 15, 6); // враги    game.load.image('bullet', 'bullet.png');  //пуля    game.load.image('backgroundimage','backgroundimage.jpg');//фон} function create() {    //главный герой    MainHero = game.add.sprite(30, 400,'MainHero');      MainHero.animations.add('walkleft', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 10, true);//changed left to walkleft    MainHero.animations.add('walkright', [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], 10, true);//changed right to walkright    upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);    leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);    rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);    //пули    bullets = game.add.group();    bullets.createMultiple(10, 'bullet');    bullets.callAll('events.onOutOfBounds.add', 'events.onOutOfBounds', resetBullet, this);      /* //enemy    Zombie = game.add.sprite(600,30,'Zombie');    Zombie.animations.add('animation name here',[frames here], FPS HERE, LOOP false/true HERE); // need to declare frames and the syntax is the wrong way around.    Zombie.animations.play('animation name here'); */  } function update() {   MainHero.body.velocity.x = 0;   MainHero.body.velocity.y = 0;   //input getting   if (leftKey.isDown)    {        MainHero.x--;        MainHero.animations.play('walkleft'); // Change from 'left' to 'walkleft' 		/* 		* The syntax for PLAYING animations is:- MainHero.animations.play('animationname');		* NOT : Mainhero.animations.play('animation name',SPEED,LOOP false/true);		*/     }    else if (rightKey.isDown)    {        MainHero.x++;        MainHero.animations.play('walkright'); // change from 'right' to 'walkright'    }     if (game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR))    {        fireBullet();    }         if (upKey.isDown && MainHero.body.touching.down)    {        MainHero.body.velocity.y = -350;    } } //отдельный модуль 2function resetBullet (bullet) {    bullet.kill();} //отдельный модульfunction fireBullet () {     if (game.time.now > bulletTime)    {        bullet = bullets.getFirstExists(false);         if (bullet)        {            bullet.reset(MainHero.x + 3, MainHero.y - 8);            bullet.body.velocity.x = +300;            bulletTime = game.time.now + 250;        }    } } //end.  /* * Change the animation name of MainHero and removed Zombie animations * As it needs frames and declared. */      // //function create() {      ///platforms = game.add.group();    ///var ground = platforms.create(0, game.world.height - 64, 'ground');    //ground.scale.setTo(2, 2);    //ground.body.immovable = true;    //var ledge = platforms.create(400, 400, 'ground');    ///ledge.body.immovable = true;     //ledge = platforms.create(-150, 250, 'ground');    //player.body.gravity.y = 6; //}//отдельный конструктор  //самое важное//функция взаимодействия 

I've modified the above code to work. Try and see. The animation syntax is wrong and back to front. The zombie has been given an animation to play but no frames declared for the animation so ive commented that out. The MainHero had it done right but the play was:

MainHero.animations.play('walk', 6, true);//Where it only needs to beMainHero.animations.play('walk');

Because the 6 and true are declared when you CREATE the frame, name and animation itself. If you want it 6 and true, declare that here:

MainHero.animations.add('walkleft', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 10, true);//changed left to walkleftMainHero.animations.add('walkright', [13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], 10, true);//changed right to walkright

After the name and the frames you have a number 10, that is the speed at which it will play ingame. You choose there whether it be 6, 10, 99999 etc etc.

 

Hope this helps :)

Link to comment
Share on other sites

Thanks, but problem doesn't left. I used debagger - nothing. I used console output, and i understand that problem is wiht last frame in sprite. You can move right, move left - it works, but when you move in 1 direction and the last sprite in spritesheet is appear app crashed. It is strange problem, becouse it must work and it doesn't! Maybe problem is with spriteshet(but it's casual)? 
And there is one more point. I'd like to zoom in Main hero, but in official guide i didn't found sth about it. Can you help me with it?
(sorry for my english, i hope i don't make so mistakes in each word
I put it on server: http://www.yaroslavkornilov.96.lt/MainGamePage.html

Link to comment
Share on other sites

Simply remove Frame 24 from the walk right and remove frame 12 from the walk left animation and add it to the walking right.

 

You have the following array as the walking left animation:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] - that's frame 0 to frame 12 (13 frames total) (but in your png you only have 12 frames for each direction)

 

Your walking right animations array:

[13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24] - that's frame 13 to 24 (12 frames total, but the index is off by 1, because of the 13 frames in the walking left animation) 

 

The correct animation arrays should look like this:

 

left:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

 

and

 

right:

[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]

 

Chaging it to this should take care of the crash, which is caused by trying to load frame number 24. (which does not exist, since you only have frames 0 to 23)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...