Created
April 21, 2012 05:53
-
-
Save wise9/2434517 to your computer and use it in GitHub Desktop.
jam.enchant.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* jam.enchant.js | |
* @version 0.3 | |
* @require enchant.js v0.4.3 or later | |
* @author sidestepism | |
* | |
* @example | |
var player = new Sprite(32, 32, 0, 0, "chara1.gif"); | |
player.touch(); | |
player.show(); | |
player.on("enterframe", function(){ | |
if(player.age % 30 == 0){ | |
var ball = new Sprite(16, 16, 160, 160, "icon1.png", rand(7)); | |
ball.vx = rand(11) - 5; | |
ball.vy = rand(11) - 5; | |
ball.hit(player, function(){ | |
ball.hide(); | |
}); | |
ball.show(); | |
} | |
}); | |
**/ | |
enchant.jam = { assets: ['effect0.gif', 'icon0.gif', 'font.png'] }; | |
enchant.jam.Sprite = enchant.Class.create((enchant.animation && enchant.animation.Sprite) || enchant.Sprite, { | |
initialize: function(width, height, x, y, image, frame){ | |
var game = enchant.Game.instance; | |
if(arguments.length == 1){ | |
console.log("hoge"); | |
(enchant.animation || enchant).Sprite.call(this, 0, 0); | |
for(var i in arguments[0])if(arguments[0].hasOwnProperty(i)){ | |
this[i] = arguments[0][i]; | |
} | |
}else{ | |
(enchant.animation || enchant).Sprite.call(this, width, height); | |
this.x = x || 0; | |
this.y = y || 0; | |
this.vx = 0; | |
this.vy = 0; | |
if(image){ | |
game.load(image); | |
this.image = game.assets[image] || null; | |
this.frame = frame || 0; | |
} | |
} | |
this.addEventListener("enterframe", function(){ | |
this.x += this.vx; | |
this.y += this.vy; | |
if(!this.scene) return; | |
}); | |
}, | |
on: function(){ | |
this.addEventListener.apply(this, arguments); | |
}, | |
emit: function(){ | |
this.dispatchEvent.apply(this, arguments); | |
}, | |
touch: function(){ | |
var sprite = this; | |
var func = function(evt){ | |
sprite.x = evt.x - sprite.width/2; | |
sprite.y = evt.y - sprite.height/2; | |
}; | |
enchant.Game.instance.rootScene.addEventListener("touchstart", func); | |
enchant.Game.instance.rootScene.addEventListener("touchmove", func); | |
enchant.Game.instance.rootScene.addEventListener("touchend", func); | |
}, | |
touchX: function(){ | |
var sprite = this; | |
var func = function(evt){ | |
sprite.x = evt.x - sprite.width/2; | |
}; | |
enchant.Game.instance.rootScene.addEventListener("touchstart", func); | |
enchant.Game.instance.rootScene.addEventListener("touchmove", func); | |
enchant.Game.instance.rootScene.addEventListener("touchend", func); | |
}, | |
touchY: function(){ | |
var sprite = this; | |
var func = function(evt){ | |
sprite.y = evt.y - sprite.height/2; | |
}; | |
enchant.Game.instance.rootScene.addEventListener("touchstart", func); | |
enchant.Game.instance.rootScene.addEventListener("touchmove", func); | |
enchant.Game.instance.rootScene.addEventListener("touchend", func); | |
}, | |
arrow: function(speed){ | |
speed = speed || 3; | |
var sprite = this; | |
var game = enchant.Game.instance; | |
game.rootScene.addEventListener("enterframe", function(){ | |
if(game.input.right) sprite.x += speed; | |
if(game.input.left) sprite.x -= speed; | |
if(game.input.down) sprite.y += speed; | |
if(game.input.up) sprite.y -= speed; | |
}) | |
}, | |
arrowX: function(speed){ | |
speed = speed || 3; | |
var sprite = this; | |
var game = enchant.Game.instance; | |
game.rootScene.addEventListener("enterframe", function(){ | |
if(game.input.right) sprite.x += speed; | |
if(game.input.left) sprite.x -= speed; | |
}) | |
}, | |
arrowY: function(speed){ | |
speed = speed || 3; | |
var sprite = this; | |
var game = enchant.Game.instance; | |
game.rootScene.addEventListener("enterframe", function(){ | |
if(game.input.down) sprite.y += speed; | |
if(game.input.up) sprite.y -= speed; | |
}) | |
}, | |
show: function(){ | |
enchant.Game.instance.rootScene.addChild(this); | |
}, | |
hide: function(){ | |
enchant.Game.instance.rootScene.removeChild(this); | |
}, | |
onhit: function(obj, func, func2){ | |
this.addEventListener("enterframe", function(){ | |
if(this.intersect(obj)){ | |
func.call(this, obj); | |
} | |
}) | |
}, | |
onhits: function(arr, func){ | |
for(var i = 0, l = arr.length; i < l; i++){ | |
if(arr[i] && this.intersect(arr[i])){ | |
func.call(arr[i]); | |
} | |
} | |
}, | |
bg: { | |
get: function(){return this.backgroundColor}, | |
set: function(color){this.backgroundColor = color} | |
} | |
}); | |
enchant.jam.Game = enchant.Class.create((enchant.nineleap && enchant.nineleap.Game) || enchant.Game, { | |
countDown: function(time){ | |
var timelabel = new enchant.util.TimeLabel(12, 12, 'countdown') | |
timelabel.time = time; | |
this.rootScene.addChild(timelabel); | |
this.timelabel = timelabel; | |
timelabel.addEventListener("enterframe", function(){ | |
this.dispatchEvent("end"); | |
}) | |
}, | |
countUp: function(){ | |
var timelabel = new enchant.util.TimeLabel(12, 12, 'countup') | |
timelabel.time = 0; | |
this.rootScene.addChild(timelabel); | |
game.timelabel = timelabel; | |
}, | |
showScore: function(){ | |
var scorelabel = new enchant.util.ScoreLabel(12, 28); | |
scorelabel.addEventListener("enterframe", function(){ | |
scorelabel.score = enchant.Game.instance.score; | |
}); | |
this.score = 0; | |
this.scorelabel = scorelabel; | |
}, | |
showPad: function(){ | |
var pad = new Pad(240, 240); | |
enchant.Game.instance.rootScene.addChild(pad); | |
}, | |
rs: { | |
get: function(){return this.rootScene}, | |
set: function(scene){this.rootScene = scene} | |
} | |
}); | |
enchant.OldScene = enchant.Scene; | |
enchant.Scene = enchant.Class.create(enchant.OldScene, { | |
bg: { | |
get: function(){return this.backgroundColor}, | |
set: function(color){this.backgroundColor = color} | |
}, | |
rs: { | |
get: function(){return this.rootScene}, | |
set: function(scene){this.rootScene = scene} | |
}, | |
on: function(){ | |
this.addEventListener.apply(this, arguments); | |
} | |
}); | |
function rand(num){ | |
return Math.floor(Math.random() * num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment