Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9060149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:09:01+00:00 2026-06-16T15:09:01+00:00

Okay, I’ve been converting my function spaghetti into revealing prototypes for my Pong game,

  • 0

Okay, I’ve been converting my “function spaghetti” into revealing prototypes for my Pong game, and haven’t had any luck drawing the paddles.

This will draw a black canvas, but the paddles refuse to draw.

I commented out the clearCanvas() to see if it was an issue there, but to no avail.

Seems like you’ll definitely want to http://jsbeautifier.org the code below, as it didn’t transfer very well =c

/*jslint browser: true, devel: true, indent: 4, undef: true, todo: true*/

canvas = document.createElement('canvas');
canvas.width = 600;
canvas.height = 400;
document.body.appendChild(canvas);
context = canvas.getContext('2d');

/////////////////////////////////////////////////////////////

var Paddle = function (playerSide) {
'use strict';
//constructor, variables go here
var x, y = 20,
    width = 10, height = 50,
    colour = '#FFF';
//Determine which paddle belongs to which player
if (playerSide === 'left') {
    this.x = 20;
} else {
    this.x = 580;
}
};

Paddle.prototype = function () {
"use strict";
//Private members
var draw = function (x, y) {
    context.fillStyle = this.colour;
    context.fillRect(x, y, this.width, this.height);
};
//Public members
return {
    draw: draw
};
}();

/////////////////////////////////////////////////////////////

var Pong = function () {
"use strict";
//constructor, variables go here
//Events
canvas.onmousemove = function (mouse) {
    this.y = mouse.pageY;
};
this.animate();
};

Pong.prototype = (function () {
"use strict";
//Private members
// requestAnim shim layer by Paul Irish
window.requestAnimFrame = (function () {
    return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function (/* function */callback, /* DOMElement */element) {
            window.setTimeout(callback, 1000 / 60);
        };
}());

var animate = function () {
        requestAnimFrame(animate);
        clearCanvas();
        drawPaddles();
    },

    drawPaddles = function () {
        leftPaddle.draw(leftPaddle.x, leftPaddle.y);
        rightPaddle.draw(rightPaddle.x, rightPaddle.y);
    },

    clearCanvas = function () {
        context.clearRect(0, 0, 600, 400);
    };

//Public members
return {
    animate: animate
};
}());

var leftPaddle = new Paddle('left');
var rightPaddle = new Paddle('right');

var pong = new Pong();

​

Any ideas what’s going on, or what concept I’m not understanding?
Also, any comments about my code otherwise would be much appreciated.

Thank you!

EDIT: I initially initialized the paddles from the Pong class, I’m assuming that’s correct, but this code has undergone so many changes as I was trying to fix the paddle issue… arrrg!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:09:02+00:00Added an answer on June 16, 2026 at 3:09 pm

    The problem seems to be with your ‘private members’, colour, width and height. These members are in the closure of some privileged member functions – and that means they can’t be accessed as properties of ‘this’.

    For instance, in the following code

    function myClass() {
        var x = 1;
        this.sayX = function() {return this.x};
        var y = 2;
        this.sayY = function() {return y};
    }
    
    var myObject = new myClass();
    
    myObject.sayX(); // returns 'undefined'
    myObject.sayY(); // returns '2'
    

    You need to either remove your references to ‘this’ in your methods that call the pseudo-private members and define the functions that call them in the same context, or make them public members of the object. Otherwise the draw functions won’t have access to the colour and dimensions of the paddles (which explains why they don’t draw).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I know that using eval() isn't great, but I haven't been able
Okay - I have a dilemma. So far my script converts page titles into
Okay, so here's my function: function getNewJobNumber($jobPrefix, $addition = 0) { $addition = $addition
Okay, so I have been working on a chat program. Its a WIP doesn't
Okay I've been trying to research how to do this and have failed. I
Okay that title may not have been too clear, I am building a site
Okay so I've been having trouble with my first XML project, so I've turned
Okay, first some background, I can't use any javascript library except YUI for this
Okay, I am working on converting some objective c code to c# here is
Okay the answer to this may be really simple but I have been searching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.