ippa / jaws

Jaws - HTML5 canvas javascript 2D Game Framework
https://jawsjs.ippa.se
GNU Lesser General Public License v3.0
363 stars 75 forks source link

method 'saved' of undefined #25

Closed krazyjakee closed 12 years ago

krazyjakee commented 12 years ago
var bubbles = new _bubbles();

$(document).ready(function() {
    var context = $('#canvas')[0].getContext("2d");
    jaws.assets.add(["img/red-bubble.png","img/yellow-bubble.png","img/blue-bubble.png","img/purple-bubble.png","img/green-bubble.png","img/black-bubble.png"]);
    jaws.start(GameState);
});

function GameState() {
    this.setup = function(){
        bubbles.loadAllAnimations();
        bubble_spritelist = jaws.SpriteList();
        bubble_spritelist.push(bubbles.red.sprite);
    }
    this.update = function() {
        bubbles.red.sprite.setImage(bubbles.red.animation.next());
    }
    this.draw = function() {
        jaws.clear();
        bubble_spritelist.draw();
    }
}

function _bubbles(){
    this.red = { sprite: new jaws.Sprite({ x:0, y:0 }), animation: [] };
    this.yellow = { sprite: new jaws.Sprite({ x:0, y:0 }), animation: [] };
    this.blue = { sprite: new jaws.Sprite({ x:0, y:0 }), animation: [] };
    this.green = { sprite: new jaws.Sprite({ x:0, y:0 }), animation: [] };
    this.purple = { sprite: new jaws.Sprite({ x:0, y:0 }), animation: [] };
    this.black = { sprite: new jaws.Sprite({ x:0, y:0 }), animation: [] };
}

_bubbles.prototype.loadAllAnimations = function(){
    bubbles.red.animation = bubbles.loadAnimation("red");;
    bubbles.yellow.animation = bubbles.loadAnimation("yellow");
    bubbles.blue.animation = bubbles.loadAnimation("blue");
    bubbles.green.animation = bubbles.loadAnimation("green");
    bubbles.purple.animation = bubbles.loadAnimation("purple");
    bubbles.black.animation = bubbles.loadAnimation("black");
}
_bubbles.prototype.loadAnimation = function(color){
    return new jaws.Animation({sprite_sheet: "img/"+color+"-bubble.png", frame_size: [40,40], frame_duration: 100});
}
krazyjakee commented 12 years ago

the break is here: bubble_spritelist.draw(); line 1229 of jaws.js

ippa commented 12 years ago

Could you provide the full example with assets and everything?

krazyjakee commented 12 years ago

Honestly, did not expect a reply, thanks very much ippa!

http://dl.dropbox.com/u/206123/chempop/index.html

Thanks again.

ippa commented 12 years ago

Looking at http://dl.dropbox.com/u/206123/chempop/js/init.js the problem seems to be this:

In the very first line, you try to create sprites before you have initialized jaws with jaws.start(). Therefore the sprites doesn't have sprite.context set.. cause when the sprites were created, it didn't jaws.context didn't exist yet.

I think the sollution is to simple move the sprite creation into setup()

If you have to create sprites before jaws.start() you can always do it with the sprite constructor new jaws.Sprite({context: your_2d_context})

krazyjakee commented 12 years ago

done, and it worked. I learned something about scope today. I also realised how awesome this engine would be for my "game engine": http://www.youtube.com/watch?v=WxS_rslCJg0

Keep up the awesome work!