Welcome to Dream.In.Code
Become a Java Expert!

Join 150,370 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,834 people online right now. Registration is fast and FREE... Join Now!




Creating instances for a game:

 
Reply to this topicStart new topic

Creating instances for a game:

Sonastylol
8 Feb, 2008 - 01:14 PM
Post #1

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 129


My Contributions
Hey everyone!

I was bored so I wanted to put my time to good use and practice Java - exercise the mind a little.
Anyway, on to the good stuff!! ~

I created a project called Adventure. There are 3 classes, Actions, Hero, and Adventure.
Actions - its going to have methods like Open Chest, Moving around, etc.
Hero - creates the heroes starting stats and will control his "Leveling Up" so-to-speak. stat upgrades, etc.
Adventure - the main class of my program. Has public static void main(String []args)

I only just started but I'll show a snippet of what I have.

Only thing I have in Adventure so far.
CODE
public static void main(String[] args)
    {
        Hero hero = new Hero();
        hero.createHero("Jeremy", 1, 40, 6, 3);
    }

import java.util.*;

This is what I have of my Hero class so far
CODE
class Hero
{
    String name = "Jeremy";
    int level;
    int HP, Str, Def;
    
    public void createHero(String name, int startLevel, int startHP, int startStr, int startDef)
    {
        level = startLevel;
        HP = startHP;
        Str = startStr;
        Def = startDef;

    }
}


Everything compiles and works so far, but I'm curious what will happen when I want hero to run some methods from the Actions class, or any other class I create?

Since Hero hero = new Hero(); creates an instance of the hero class, can I do a call hero.MethodFromActionsClass(); ??

I'm going to be stuck if that doesnt work. Thank you.


can you please explain to me what is wrong with that if it DOESN'T work?


User is offlineProfile CardPM
+Quote Post

potator
RE: Creating Instances For A Game:
8 Feb, 2008 - 01:35 PM
Post #2

D.I.C Head
Group Icon

Joined: 2 Dec, 2007
Posts: 78



Thanked: 1 times
Dream Kudos: 175
My Contributions
you don't need to call method like:

hero.methodFromOtherClass();

when you write methods in the Action class, make them public and static. To call them, you simply need to say:

methodFromActionClass();

This should be OK as long as you don't have more that one player at a time because anything you find or pick up will go into the only inventory anyway.
User is offlineProfile CardPM
+Quote Post

Sonastylol
RE: Creating Instances For A Game:
8 Feb, 2008 - 01:49 PM
Post #3

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 129


My Contributions
WOW! Can I really just do method();

without a xxxxxx.method(); ??

Ive been thru many semesters of java at college already and never knew I could do that x_x


Thanks so much. I'll try it out.


SO what you're saying is that I have to make it.


CODE
public class Actions
{
     public static Loot()
          {
               //item+1   OR  System.out.println("You have looted:"+item);
          }
}



This is legal? What if I want something to happen to "hero" within that method? Is that legal?


User is offlineProfile CardPM
+Quote Post

potator
RE: Creating Instances For A Game:
8 Feb, 2008 - 02:02 PM
Post #4

D.I.C Head
Group Icon

Joined: 2 Dec, 2007
Posts: 78



Thanked: 1 times
Dream Kudos: 175
My Contributions
QUOTE(Sonastylol @ 8 Feb, 2008 - 02:49 PM) *

CODE
public class Actions
{
     public static Loot()
          {
               //item+1   OR  System.out.println("You have looted:"+item);
          }
}



well, you'd need a return type or a void return type for your code to compile. Also, you'd need to have the two files saved in the same folder or make action part of a package that hero imports. The latter is more traditional way to handle this. Note: you may have to call it as:

CODE

action.Loot();


the problem with the sample method that you gave is that it does not apply to more than one object: i.e loot(chest) vs. loot(body)

This post has been edited by potator: 8 Feb, 2008 - 02:03 PM
User is offlineProfile CardPM
+Quote Post

Sonastylol
RE: Creating Instances For A Game:
8 Feb, 2008 - 02:39 PM
Post #5

D.I.C Head
**

Joined: 15 Dec, 2007
Posts: 129


My Contributions
I just made a quick Loot() method in the Actions class. I just had it print a line and it worked! Actions.Loot();

Awesome thanks a lot!

I have a question - since this is going to be a project of mine, how would you create an inventory..?

it seems rather complex. An array starting like [10] for a slot 0-9, with each slot as a reference to an item or something?


or String slot1 = xxxxx;
String slot2 = xxxxx;
String slot3 = xxxxx;


This second method seems awful because it just names something, it doesnt actually have an item property. Yikes!

Any ideas? Thanks so much
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:21PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month