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 4610114
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:05:08+00:00 2026-05-22T01:05:08+00:00

A few days ago, I started working with cocos2d. I really like the framework.

  • 0

A few days ago, I started working with cocos2d. I really like the framework. I would like to create a game with cocos2d and have a probably simple question…

I am making a game with 4 characters, which all have similar characteristics, but have some different attributes like “type” and “points”. I’d like to subclass the sprites into one class which handles all their logic, drawing, and animation.

My question though, is how do I call the sprite class with say, a “type” parameter of 1, 2, 3, or 4 and then have the class draw the correct sprite into my scene with all of it’s individual logic?

Thanks!

  • 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-22T01:05:08+00:00Added an answer on May 22, 2026 at 1:05 am

    You should have an Enemy class that contains properties of specific enemies and that are not type specific (like position, current health, a CCSprite instance?) and an EnemyType class that contains properties that are shared among all enemies of a specific type (max health, max speed, size, sprite filename). You should load your enemy types prior to loading the level, than instantiate each enemy using the appropriate type in the constructor.

    For example if your enemy element in the level file looks like this

    <enemy><type>spider</type>...more properties...</enemy>
    

    The code (pseudo) would do something like

       EnemyType *enemyType = nil;
       if (typeElement.value == "spider")
       {
           enemyType = spiderType;
       }
       Enemy *newEnemy = [Enemy enemyWithType:enemyType];
    

    Also the Enemy class should contain the CCSprite that represents it, not subclass it. An enemy is not a sprite, so unless I’m missing something, as i see it, an enemy should not inherit from a sprite. I’d read about when to contain and when to inherit.

    EDIT:
    Another nice post to read that seems very relevant and could communicate a few other things probably better than me.

    Anyway, my intention was not to make you think you should just rethink your entire design. What i’m proposing is “better” MVC-wise, but it doesn’t mean it’s better for your game. If you spend all your time on “design correctness” you’ll never get a game done, especially if you’re just learning the cocos2d framework, i was actually making a learning project not too long ago and Steve McConnel himself would come over and slap me if he saw it.

    If you’re working on a game alone and it’s a small project go ahead and subclass away if it’s going to be more manageable to you, everything, including “design correctness” needs to be properly quantified (except maybe usage of “goto” statements 🙂 ).

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

Sidebar

Related Questions

Just started working with Mercurial a few days ago and there's something I don't
I started to use Borland's Turbo C++ a few days ago. I actually have
I ordered Programming Windows Fifth Edition a few days ago, and started working with
I started using Raphael.js a few days ago and I'm really enjoying it. The
I started using django framework just a few days ago and i desperately need
I started programming in Lua few days ago. I have become familiar with the
I have started to use jqGrid in our project a few days ago and
I just started using Kohana a couple days ago, and I have a few
I just started learning Common Lisp a few days ago, and I'm trying to
So far I have worked in the console and a few days ago decided

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.