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

  • SEARCH
  • Home
  • 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 6866549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:11:53+00:00 2026-05-27T03:11:53+00:00

This is probably very basic stuff but I’m stuck and a bit clueless. I

  • 0

This is probably very basic stuff but I’m stuck and a bit clueless.

I wrote a Snake game recently in CoffeeScript, but I’m not at all happy with the way I structured the code.

I’m trying to rewrite this using classes. I have a Game class, a Snake class, a Food class. It makes sense in my head to do it this way. However, to access variables and functions, I’m using the ‘@’ (this) keyword, and I feel like I’m completely overdoing it. For example, I have a list of constants at the top of my Game class. I had to declare all of them as @CONSTANT to be able to access them. Or, inside of a method:

clearCanvas: ->
    @ctx.clearRect 0, 0, @canvas.width, @canvas.height

The same goes for method calls. For example:

init: ->
    window.snake = new Snake()
    @showStartScreen()

Can someone tell me if this is okay to do, or how I should structure my code? I can put a link up to the code I’m working with at the moment, if someone would be so kind to review it for me. (Edit: actually here it is)

Thanks a lot.

  • 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-05-27T03:11:53+00:00Added an answer on May 27, 2026 at 3:11 am

    If you’re using CoffeeScript classes you’ll be making heavy use of @. Any properties on your objects must be accessed through @ or this. Here’s how you should be using constants:

    class Game
      up:     1
      right:  2
      down:   4
      left:   8
    
      someMethod: ->
        console.log @up, @right, @down, @left
    
    console.log "Class accessible #{Game::up}"
    
    g = new Game
    console.log "Object accessinble #{g.up}"
    
    g.someMethod()
    

    Also, @thejh is right. You should be using soft tabs at 2 space width.

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

Sidebar

Related Questions

This is probably very basic stuff, but please bear in mind I am completely
This is probably a very basic question, but I could not get my way
This is probably something very basic, but my Erlang installation is doggedly rejecting my
This is probably very basic, but it's giving me a headache, and I'm not
This probably is a very very basic question but i can't seem to find
This is probably a very basic question but I haven't found any detailed information
All, This is probably very basic, but I cannot find an answer that works
Probably a very basic solution to this, but I can't seem to figure it
I realize that this is probably a very basic question, but I have spent
This is probably a very basic question, but I'm really new to generics in

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.