Skip to content

Instantly share code, notes, and snippets.

@sfentress
Created August 23, 2012 15:53
Show Gist options
  • Save sfentress/3437951 to your computer and use it in GitHub Desktop.
Save sfentress/3437951 to your computer and use it in GitHub Desktop.
{"libraries":[]}
div {
position: relative;
top: 0;
left: 0;
}
#wrapper {
background: url('http://sfentress.github.com/GGExperiment/resources/images/sky.jpg');
overflow: hidden;
border: 5px solid #402309;
height: 510px;
}
#mountains {
background: url('http://sfentress.github.com/GGExperiment/resources/images/mountains.png');
background-position: 0px 0px;
height: 700px;
width: 2000px;
top: -90px;
}
#grass {
background: url('http://sfentress.github.com/GGExperiment/resources/images/grass.png');
background-position: 0px 0px;
height: 700px;
width: 2000px;
top: -90px;
position: absolute;
}
#dragon {
background: url('http://sfentress.github.com/GGExperiment/resources/images/drakesprite.png');
background-size: 100%;
width: 549px;
height: 439px;
background-repeat: no-repeat;
position: absolute;
}
<html>
<head>
<title>Flying Drake</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div id="wrapper">
<div id="mountains"></div>
<div id="grass"></div>
<div id="dragon"></div>
</div>
</body>
</html>
$(function() {
// extra code required because livecoding will
// reload all setIntervals each time...
if (typeof interval1 !== "undefined") clearInterval(interval1);
if (typeof interval2 !== "undefined") clearInterval(interval2);
if (typeof interval3 !== "undefined") clearInterval(interval3);
// end extra
interval1 = setInterval('animateSprite()',100);
interval2 = setInterval('moveSprite()', 1500);
interval3 = setInterval('moveBackground()', 50);
});
var spriteFrame = 1;
animateSprite = function() {
var top = 439 * (spriteFrame % 8);
$('#dragon').css('backgroundPosition','-0px -'+top+'px');
spriteFrame++;
};
var moveFrame = 0;
var movementSequence = [[20,100], [50,80], [20,100], [60, 160], [40, 160], [60,100], [80, 70], [50, 40]];
moveSprite = function() {
var movement = movementSequence[moveFrame % movementSequence.length];
$('#dragon').animate({
left: movement[0],
top: movement[1]
}, 1000);
moveFrame++;
}
moveBackground = function() {
$('#mountains').css({'backgroundPosition': '-=5px -0px'});
$('#grass').css({'backgroundPosition': '-=20 -0px'});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment