java - How to make a Person keep a reference to a Player in this code? -
problem: when skeleton enters place, want players screens updated using method in world object:
/** * `text' in place `place'. text become visible @ * bottom of text window of players watching `place'. * * @param place * place string displayed. * @param text * string diplayed. */ public void sayatplace(place place, string text) { synchronized (players) { iterator<player> ls = players.iterator(); while (ls.hasnext()) { player p = ls.next(); if (p.currentplace() == place) { p.say(text); } } } } i've got 2 classes, person , player , want person write textarea when method goto called can't make person object have proper reference player has textarea:
package adventure; import java.awt.*; import java.util.*; /** * adt persons completed subclasses creating actors * specific properties */ public class person { public player player = null; /** * name of person. */ public string name; /** * world person associated with. */ public world world; /** * tells person @ moment. */ public place where; /** * create person named `name' in world `world'. * * @param world * world person created. * @param name * name of person. * @param app * image used display person. */ public person(world world, string name, image app) { this.world = world; this.name = name; this.appearance = app; // this.player = player; = world.defaultplace(); where.enter(this); inventory = collections.synchronizedcollection(new linkedlist<thing>()); } /** * go directly, , quietly, 'place'. say, without posting * message you're doing so. * * @param place * string referring place go to. * @see #goto(place) * @see #go */ public void goto(string place) { goto(world.getplace(place), null); } /** * go directly, , quietly, `whereto'. say, without posting * message you're doing so. * * @param whereto * place go to. can null in case nothing happens. * @see #goto(string) * @see #go */ public void goto(place whereto, player player) { if (whereto != null) { where.exit(this); whereto.enter(this); // update player's viewing place `where'. world.update(where); // record our new position. = whereto; // update player's here. world.update(where); } system.out.println("player:"+player); if(player != null){ player.say("a terrifying skeleton warrior appears!"); } // send msg doors available object[] doornames = whereto.exits.keyset().toarray(); string s = ""; int = 1; (object obj : doornames) { if (i < doornames.length) { s = s + obj.tostring().tolowercase(); if(i<doornames.length){ s = s+ ","; } } if (i == doornames.length && > 1) { s = s + " , " + obj.tostring().tolowercase(); } if (i == doornames.length && == 1) { s = obj.tostring().tolowercase(); } ++i; } if (player != null) { player.say("there doors " + s); } } package adventure; import java.awt.*; /** * player object enables commands control person: »you». * object displayed graphically control panel user both can * control , follow course of events */ public class player extends panel { private person me; private placeview placeview; private commands commands; private textarea textarea; private static final long serialversionuid = 100l; /** * creates new instance of player, in world w, reflecting person p. * * @param w * world in player play in. * @param p * person associated player. */ player(world w, person p) { setlayout(new borderlayout()); setsize(650, 540); me = p; p.player = this; placeview = new placeview(me); commands = new commands(me); textarea = new textarea("", 10, 60, textarea.scrollbars_vertical_only); textarea.append("you in dungeon. horrible shrieks of undead chill bones.\n"); textarea.seteditable(false); add("west", placeview); add("east", commands); add("south", textarea); w.addplayer(this); } /** * display string in players graphical interface. * * @param text * string display. */ void say(string text) { textarea.append(text + '\n'); textarea.repaint(); } /** * returns place of person associated player. * * @return place of person associated player. */ public place currentplace() { return me.where; } } do understand i'm trying do? code want work is
system.out.println("player:"+player); if(player != null && this.name.equals("skeleton")){ player.say("a terrifying skeleton warrior appears!"); } but player reference of person zombie not instanciated. person has player object instanciated player person.
can me? posted full versions of person, player , world classes here
http://pastebin.com/rjccr2ph (person) http://pastebin.com/eysh8l9q (player) http://pastebin.com/dkvrvey8 (world)
if understand question well, want have 2 classes in each class has reference other. need create 1 of them (let foo), create other class (let boo) , pass reference of foo via constructor, , set reference of boo inside foo class via setter method.
for example:
public static void main(string[] args) { foo f = new foo(); boo b = new boo(f); f.setboo(b); } class foo { private boo b; public foo() { // ... } public void setboo(boo b) { this.b = b; } } class boo { private foo f; public boo(foo f) { this.f = f; } } now, foo has reference of boo, , boo has reference of foo :)
Comments
Post a Comment