Jump to content

HTML5 game: making jet shoot red bullet but is green?


allc1865
 Share

Recommended Posts

Hi. I'm trying to make my jet shoot red bullets, but for some reason they're coming out at green.

Here's my code....

function Jet() {    this.srcX = 0;    this.srcY = 510;    this.width = 100;    this.height = 40;    this.speed = 3;    this.drawX = 220;    this.drawY = 200;    this.noseX = this.drawX + 100;    this.noseY = this.drawY + 30;    this.isUpKey = false;    this.isRightKey = false;    this.isDownKey = false;    this.isLeftKey = false;    this.isSpacebar = false;    this.isShooting = false;    this.bullets = [];    this.currentBullet = 0;    for (var i = 0; i < 25; i++) {        this.bullets[this.bullets.length] = new Bullet();    }}Jet.prototype.draw = function() {    clearCtxJet();    this.checkDirection();    this.noseX = this.drawX + 100;    this.noseY = this.drawY + 30;    this.checkShooting();    this.drawAllBullets();    ctxJet.drawImage(imgSprite, this.srcX, this.srcY, this.width, this.height, this.drawX, this.drawY, this.width, this.height);};Jet.prototype.checkDirection = function() {    if (this.isUpKey) {        this.drawY -= this.speed;    }    if (this.isRightKey) {        this.drawX += this.speed;    }    if (this.isDownKey) {        this.drawY += this.speed;    }    if (this.isLeftKey) {        this.drawX -= this.speed;    }};Jet.prototype.drawAllBullets = function() {    for (var i = 0; i < this.bullets.length; i++) {        if (this.bullets[i].drawX >= 0) this.bullets[i].draw();        if (this.bullets[i].explosion.hasHit) this.bullets[i].explosion.draw();    }};Jet.prototype.checkShooting = function() {    if (this.isSpacebar && !this.isShooting) {        this.isShooting = true;        this.bullets[this.currentBullet].fire(this.noseX, this.noseY);        this.currentBullet++;        if (this.currentBullet >= this.bullets.length) this.currentBullet = 0;    } else if (!this.isSpacebar) {        this.isShooting = false;    }};function clearCtxJet() {    ctxJet.clearRect(0, 0, gameWidth, gameHeight);}// end of jet functions// bullet functionsfunction Bullet() {    this.srcX = 100;    this.srcY = 460;    this.drawX = -20;    this.drawY = 0;    this.width = 5;    this.height = 5;    this.explosion = new Explosion();}Bullet.prototype.draw = function() {    this.drawX += 3;    ctxJet.drawImage(imgSprite, this.srcX, this.srcY, this.width, this.height, this.drawX, this.drawY, this.width, this.height);    this.checkHitEnemy();    if (this.drawX > gameWidth) this.recycle();};Bullet.prototype.fire = function(startX, startY) {    this.drawX = startX;    this.drawY = startY;};Bullet.prototype.checkHitEnemy = function() {    for (var i = 0; i < enemies.length; i++) {        if (this.drawX >= enemies[i].drawX &&            this.drawX <= enemies[i].drawX + enemies[i].width &&            this.drawY >= enemies[i].drawY &&            this.drawY <= enemies[i].drawY + enemies[i].height) {                this.explosion.drawX = enemies[i].drawX - (this.explosion.width / 2);                this.explosion.drawY = enemies[i].drawY;                this.explosion.hasHit = true;                this.recycle();                enemies[i].recycleEnemy();        }    }};Bullet.prototype.recycle = function() {    this.drawX = -20;};// end of bullet functions// explosion functionsfunction Explosion() {    this.srcX = 728;    this.srcY = 520;    this.drawX = 0;    this.drawY = 0;    this.width = 50;    this.height = 50;    this.hasHit = false;    this.currentFrame = 0;    this.totalFrames = 10;}Explosion.prototype.draw = function() {    if (this.currentFrame <= this.totalFrames) {        ctxJet.drawImage(imgSprite, this.srcX, this.srcY, this.width, this.height, this.drawX, this.drawY, this.width, this.height);        this.currentFrame++;    } else {        this.hasHit = false;        this.currentFrame = 0;    }};// end of explosion functions// enemy functionsfunction Enemy() {    this.srcX = 0;    this.srcY = 559;    this.width = 100;    this.height = 40;    this.speed = 2;    this.drawX = Math.floor(Math.random() * 1000) + gameWidth;    this.drawY = Math.floor(Math.random() * 360);}Enemy.prototype.draw = function() {    this.drawX -= this.speed;    ctxEnemy.drawImage(imgSprite, this.srcX, this.srcY, this.width, this.height, this.drawX, this.drawY, this.width, this.height);    this.checkEscaped();};Enemy.prototype.checkEscaped = function() {    if (this.drawX + this.width <= 0) {        this.recycleEnemy();    }};Enemy.prototype.recycleEnemy = function() {    this.drawX = Math.floor(Math.random() * 1000) + gameWidth;    this.drawY = Math.floor(Math.random() * 360);};

Any help would be gladly appreciated!! :)  :)

Link to comment
Share on other sites

As there are no actual colours in your code it can only be a problem with the imgSprite values, i.e. you're cutting out the wrong part of it, or it's a problem in another piece of code somewhere else in the game. Impossible to say for sure.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...