Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Intro to objects using a simple ball juggling concept.
  • Loading branch information
tim-white committed Sep 21, 2013
1 parent d94d20d commit 826ea74
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
Binary file added JuggleBalls/Week1/CoderDojo-ball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions JuggleBalls/Week1/JuggleBalls.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<script type="text/javascript" src="animatedObject.js"></script>
<script type="text/javascript" src="oneBall.js"></script>
</head>
<body>
</body>
</html>
50 changes: 50 additions & 0 deletions JuggleBalls/Week1/animatedObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function animatedObject( imageName, width, height ) {
this.imgName = imageName;
this.hOffset = 4;
this.vOffset = 4;

this.left = 0;
this.top = 0;

this.imageObject = document.createElement("img");
this.imageObject.src = imageName;
this.imageObject.width = width;
this.imageObject.height = height;
this.imageObject.style.position = "absolute";

this.setPosition = function(xPos, yPos) {
this.left = xPos;
this.top = yPos;
if ( this.imageObject.parentElement != null ) {
this.imageObject.style.left = this.imageObject.parentElement.offsetLeft + xPos + "px";
this.imageObject.style.top = this.imageObject.parentElement.offsetTop + yPos + "px";
}
}

this.get

this.shouldChangeHDir = function() {
var changeDir = false;
if ( this.left + this.hOffset + this.imageObject.clientWidth > this.imageObject.parentElement.clientWidth )
changeDir = true;
else if ( this.left + this.hOffset < 0 )
changeDir = true;
return changeDir;
}

this.shouldChangeVDir = function() {
var changeDir = false;
if ( this.top + this.vOffset + this.imageObject.clientHeight > this.imageObject.parentElement.clientHeight )
changeDir = true;
else if ( this.top + this.hOffset < 0 )
changeDir = true;
return changeDir;
}

this.animate = function() {
if ( this.shouldChangeHDir() ) this.hOffset = -this.hOffset;
if ( this.shouldChangeVDir() ) this.vOffset = -this.vOffset;

this.setPosition( this.left + this.hOffset, this.top + this.vOffset );
}
}
6 changes: 6 additions & 0 deletions JuggleBalls/Week1/jquery-1.10.2.min.js

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions JuggleBalls/Week1/oneBall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var gameCanvas = null;

var ball;

function setup() {
ball = new animatedObject("CoderDojo-ball.png",50,50);
getCanvas().appendChild(ball.imageObject);
ball.setPosition( Math.floor(Math.random()* (getCanvas().clientWidth+1)), Math.floor(Math.random()*(getCanvas().clientHeight+1)));

animate();
}

function animate() {
ball.animate();
setTimeout(animate,20);
}

function getCanvas() {
if ( gameCanvas == null ) {
gameCanvas = document.body;
}
return gameCanvas;
}

window.onload = function() {
setup();
}

0 comments on commit 826ea74

Please sign in to comment.