Skip to content

Instantly share code, notes, and snippets.

@DuffleOne
Created October 16, 2012 20:18
Show Gist options
  • Save DuffleOne/3901714 to your computer and use it in GitHub Desktop.
Save DuffleOne/3901714 to your computer and use it in GitHub Desktop.
Javascript Fish Class
// JavaScript Document
function Fish(na, im, w, h, sw, sh, sf) {
/* Private Var Declarations */
var name = na;
var image = im;
var height = h;
var width = w;
var start_height = sh;
var start_width = sw;
var start_flipped = sf;
var going_right = true;
var going_down = true;
var object = null;
/* Public Var Declarations */
this.container = $('#fish_cont');
this.change = 6000;
this.speed = 50;
this.ymax = window.innerHeight;
this.xmax = window.innerWidth;
/* Private Method Declarations */
var move_to = function move_to(obj, x, y) {
if (obj==null) {
obj = object;
}
obj.css('left', x + 'px');
obj.css('top', y + 'px');
}
var move_by = function move_by(obj, x, y) {
if (obj==null) {
obj = object;
}
obj.css('left', (parseInt(obj.css('left')) + x) + 'px');
obj.css('top', (parseInt(obj.css('top')) + y) + 'px');
}
var checkdir = function checkdir(obj) {
if ( going_right ) {
obj.addClass('flip');
} else {
obj.removeClass('flip');
}
}
/* Public Method Declarations */
this.create = create;
this.bobble = bobble;
this.destroy = destroy;
this.obj = get_object;
/* Methods */
function create() {
this.container.append('<img id="f_'+name+'" src="'+image+'" class="fish '+ (start_flipped?'flip':'') +'" style="height:'+height+';width:'+width+';top:'+start_height+';left:'+start_width+';">');
object = $('#f_'+name);
}
function bobble() {
incrx = 0;
incry = 0;
incrx=Math.floor(Math.random() * (this.xmax - parseInt(width)));
incry=Math.floor(Math.random() * (this.ymax - parseInt(height)));
console.log(incrx);
console.log(incry);
going_right = (incrx <= parseInt(object.css('left')))?false:true;
checkdir(object);
object.animate({
left: incrx + 'px',
top: incry + 'px'
}, 1000, function() {
bobble();
});
}
function destroy() {
}
function get_object() {
return object;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment