Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Mario is introduced to gravity and jumping

gave mario the ability to jump in an environment effected by gravity
using a new player class.
  • Loading branch information...
commit 6bafeb09acdb97240750417dc774319dcdf225df 1 parent dd523ba
@usernameusername usernameusername authored
View
6 framework.js
@@ -310,12 +310,18 @@ Animation = function(width, height, row, column, limit, imgSrc, fps, columns, ro
{
this.row = num;
this.rowStart = num;
+
+ this.cropPosition.x = this.width * this.column;
+ this.cropPosition.y = this.height * this.row;
};
this.SetColumn = function(num)
{
this.column = num;
this.columnStart = num;
+
+ this.cropPosition.x = this.width * this.column;
+ this.cropPosition.y = this.height * this.row;
};
this.Update = function()
View
1  index.html
@@ -10,6 +10,7 @@
Your browser does not support HTML5 canvas. If you would like to view, please update your browser.
</canvas>
+ <script src="player.js"></script>
<script src="main.js"></script>
</body>
</html>
View
20 main.js
@@ -6,12 +6,30 @@ canvas.style.width = canvas.width + "px";
canvas.style.height = canvas.height + "px";
input.offset = new Vector2(GetLeft(canvas), GetTop(canvas));
+var player = new Player();
+
+var floor = new Rectangle(100, 400, 300, 20);
+floor.color = new Color(0, 0, 0, 1);
+
+var gravity = 2;
+
var Update = setInterval(function()
{
-
+ player.Update();
+
+ if (player.rect.Intersects(floor) && !player.jumping)
+ {
+ player.SetPosition(null, floor.y - player.rect.height);
+ player.mayJump = true;
+ }
+ else
+ player.mayJump = false;
}, 1);
var Draw = setInterval(function()
{
ctx.clearRect(0, 0, canvas.width, canvas.height)
+
+ floor.Draw(ctx)
+ player.Draw(ctx)
}, 33);
View
BIN  mario.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
88 player.js
@@ -0,0 +1,88 @@
+Player = function()
+{
+ this.rect = new Rectangle(250, 0, 16, 16);
+ this.rect.color.r = 0;
+ this.rect.color.g = 0;
+
+ this.animation = new Animation(16, 16, 0, 0, 8, "mario.png", 12, 4, 5);
+
+ this.speed = 1.5;
+ this.moving = false;
+ this.mayJump = false;
+ this.jumping = false;
+ this.JUMP_MAX = 2.5;
+ this.jumpVelocity = 0;
+
+ this.SetPosition = function(x, y, mod)
+ {
+ if (mod == null || !mod)
+ {
+ if (x != null)
+ this.rect.x = x;
+ if (y != null)
+ this.rect.y = y;
+ }
+ else
+ {
+ if (x != null)
+ this.rect.x += x;
+ if (y != null)
+ this.rect.y += y;
+ }
+ }
+
+ this.Jump = function()
+ {
+ if (this.mayJump)
+ {
+ this.jumping = true;
+ this.jumpVelocity = this.JUMP_MAX;
+ }
+ }
+
+ this.Update = function()
+ {
+ this.moving = false;
+
+ if (input.left || input.a)
+ {
+ this.animation.SetRow(2);
+ this.rect.x -= this.speed;
+ this.moving = true;
+ }
+ if (input.right || input.d)
+ {
+ this.animation.SetRow(0);
+ this.rect.x += this.speed;
+ this.moving = true;
+ }
+ if (input.up || input.w)
+ this.Jump();
+ if (this.jumping)
+ {
+ this.rect.y -= this.jumpVelocity;
+ this.jumpVelocity -= 0.02;
+
+ if (this.jumpVelocity <= 0)
+ {
+ this.jumping = false;
+ this.mayJump = true;
+ }
+ }
+ else
+ this.rect.y += gravity;
+
+ this.animation.position.Set(this.rect.x, this.rect.y)
+
+ if (this.moving)
+ this.animation.Update();
+ else
+ this.animation.SetColumn(0);
+ }
+
+ this.Draw = function(ctx)
+ {
+ //this.rect.Draw(ctx);
+ this.animation.Draw(ctx);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.