[XNA] creating a simple class

Started by
6 comments, last by daviangel 13 years, 11 months ago
im learning XNA from the book (http://www.amazon.com/dp/0596521952/?tag=rbookshop-20) and im up to chapter 4... in chapter 4 is when the author introduces 'object oriented design' and instructs me to create a Class file with the properties for Sprites i create. Only thing is after i create the Class files in the content pipline and add all the properties the book tells me, i compile, run, and... nothing what i'm trying to find out is how do you load the class into the main game file, what do i have to create in the main file to actually see the class in action... this is the exact class file im working with (http://mastrgamr.pastebin.com/xnzUD4iG)
[size="4"]-- [size="4"]Stuart, [size="2"]Currently: Messing with LibGDX (Java)
aspiring video game programmer. (follow @mastrgamr)
Programming on and off since 2008 in C++, and C# (XNA).
Attending college as a Computer Science major.
Advertisement
First, what you have defined is a so-called abstract class, which cannot be instatiated ("used") at all. You got the abstract keyword both on the class and on the property direction. Abstract classes, similar to interfaces, defer implementations. Apart from that, a class declaration alone won't do anything before you actually instiate instantiate/create (EDIT: Sorry for that horrible typo) it or access it in other ways from your main code.

If you have never created/defined own classes yet, these explanations won't help you much. And I for one wouldn't start teaching OO with an abstract class example. But honestly I don't know the book and what knowledge it assumes or if you even misread/misunderstood something. Actually
Quote:...create the Class files in the content pipline...
sounds very odd to me. No offence intended, neither to you nor to the author.

So before helping you further please tell us how experienced you are and what the intention of the chapter is, namely:
- Create a class for abstrahating a (animated) 2D sprite ?
- Drawing one ore more of them in your drawing loop ?
- Loading the sprite through the content pipeline ?

And maybe a short description of the first chapters might be enlighting, too.

unbird

(EDIT)
PS: Sorry, forgot about google books. Ok, from a quick glance, you just did not read enough, because later in the chapter so-called inheritance is used to declare actually usable classes. Just read on and type/copy ALL the code from that chapter and something will show up.

[Edited by - unbird on May 1, 2010 5:53:42 PM]
Quote:Original post by unbird

So before helping you further please tell us how experienced you are and what the intention of the chapter is, namely:
- Create a class for abstrahating a (animated) 2D sprite ?
- Drawing one ore more of them in your drawing loop ?
- Loading the sprite through the content pipeline ?


Well this is what i know how to do, overall and what was taught in the first 3 chapters:
-drawing sprites
-animating sprites, with sprite sheets (using the update() function)
-getting user input, for windows game
-simple collision detection (using rectangles and checking when they intersect)

immma paraphrase a little what the author wrote out in the beginnng of the chapter.. basically it tells me that most of the sprites i've been using so far (chapters 1-3) share common functions like user input, animating.
quoted: "Based on that logic, it would make sense to create a base class that represents
a standard sprite and has the ability to draw itself and animate via a sprite sheet."

if i had to guess what its trying to teach me is, everytime i create a sprite i can use the 'base sprite' class to give it properties (i guess drawing and animating by itself)

and by the content pipeline i mean the solution explorer, under the Content folder

sorry im bad at explaining it.. i just have no clue what it's having me do.
[size="4"]-- [size="4"]Stuart, [size="2"]Currently: Messing with LibGDX (Java)
aspiring video game programmer. (follow @mastrgamr)
Programming on and off since 2008 in C++, and C# (XNA).
Attending college as a Computer Science major.
Quote:sorry im bad at explaining it.. i just have no clue what it's having me do.


No problem, we can work this out.

I think you're just overwhelmed by all the different stuff that's going on, or maybe it's even too advanced or fast paced for you.
Read on, I don't think it will help you if I reexplain the chapter. If you got specific questions, ask, but really be specific.

Sorry to ask again, I'm still uncertain what experience you have:
- How long have you been programming and with what language(s) ?
- What have you programmed already (apart from stuff from that book) on your own?
- Have you already programmed with graphics, for instance .NET GDI ?
- How well do you understand object-oriented design or programming (if at all):
- Have you ever done a class by yourself ?
- Do you know what an instance is and how it is created ?
- Do you know what inheritance is ?
Stuart, I suggest you go to MSDN first and work through the C# tutorials there. They are short and to the point, illustrating all the language features, and how to use them.

Then you'll be better prepared to understand your XNA book. Trying to learn C# and XNA game programming at the same time is a crazy uphill battle.
Quote:Original post by unbird
Sorry to ask again, I'm still uncertain what experience you have:
- How long have you been programming and with what language(s) ?
- What have you programmed already (apart from stuff from that book) on your own?
- Have you already programmed with graphics, for instance .NET GDI ?
- How well do you understand object-oriented design or programming (if at all):
- Have you ever done a class by yourself ?
- Do you know what an instance is and how it is created ?
- Do you know what inheritance is ?


-i been programming on and off for a lil over a year in C++, i only know the very basics of console C++ programming, functions, arrays, pointers, stuff like that
-programmed in C#? almost nothing :x ... i borrowed Sam's teach yourself in 21 days but only got up to day 7 until i had to return the book
-never programed with graphics, but i understand the concept of 2d graphics for XNA
-i done simple classes and structs in C# (from the Sam's book)
-don't know what inheritance is

based on all the doubtful answers ill take Daaark's advice >_>
[size="4"]-- [size="4"]Stuart, [size="2"]Currently: Messing with LibGDX (Java)
aspiring video game programmer. (follow @mastrgamr)
Programming on and off since 2008 in C++, and C# (XNA).
Attending college as a Computer Science major.
I second Daaark remark. Get that other book back or learn from non-graphic tutorials.

If you think you can take the challenge, though: Make your Sprite class non-abstract and work from there.

Quote:Original post by Daaark
Stuart, I suggest you go to MSDN first and work through the C# tutorials there. They are short and to the point, illustrating all the language features, and how to use them.

Then you'll be better prepared to understand your XNA book. Trying to learn C# and XNA game programming at the same time is a crazy uphill battle.

Yes there will be a number of things that will confuse you if all you used was C++. Things like foreach loop, properties and interfaces, etc. If you knew Java it wouldn't be as bad since Java is a lot more similar to C#.
Get the Petzold free C# ebook to get up to snuff on the basics of C# and .NET.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

This topic is closed to new replies.

Advertisement