我有一个1v1游戏,我正在更新为3个玩家(最终4个),当玩家正在与2个电脑玩家对战时,我遇到了玩家跟踪问题。
我有一个游戏NSObject,可以处理游戏玩法的所有主要代码,还可以控制计算机玩家的操作。在过去,我在Game.h(User和Opponent)中有2个属性,现在我有3个(User,Opponent,Opponent2)。声明如下:
@property (retain) Player *user;
当我需要使用控制计算机播放器操作的方法时,会出现问题。例如,当计算机播放器需要出价时,它必须知道出价和其他关于它的对手的信息。旧代码的片段是:
- (void)computerBiddingObjects {
// ... other stuff
if (_opponentPoints == 110 && _userPoints >= 90 && _userDidBid != YES) {
bid = bidTwenty;
} else if (_opponentPoints == 115 && _userPoints >= 90 && _userDidBid != YES) {
bid = bidTwentyFive;
}
// ... other stuff
}
为了获得对方球员的信息,我想到的是向Player.h添加2个新属性,以便跟踪每个玩家的对手(对手1和对手2)。我的问题是,如果我将每个播放器的属性设置为:
_opponent.opponent = _user;
_opponent.opponent2 = _opponent2;
我可以在两种情境中引用它们和/或设置它们各自的属性吗?鉴于上述情况,这些陈述是否相同:
_opponent.opponent.didBid = YES;
_user.didBid = YES;
另外,我是否可以访问播放器代码片段这个新版本的播放器对象属性?
- (void)computerBiddingObjectsForOpponent:(Player *)opponent {
// ... other stuff
if (opponent.points == 110 && self.currentBid < bidTwenty && ((opponent.opponent1.points >= 90 && opponent.opponent1.didBid != YES) || (opponent.opponent2.points >= 90 && opponent.opponent2.didBid != YES))) {
bid = bidTwenty;
} else if (opponent.points == 115 && self.currentBid < bidTwentyFive && ((opponent.opponent1.points >= 90 && opponent.opponent1.didBid != YES) || (opponent.opponent2.points >= 90 && opponent.opponent2.didBid != YES))) {
bid = bidTwentyFive;
}
// ... other stuff
opponent.bid = bid.
}
我离开基地/偏离轨道了吗?有更简单的方法吗?
答案 0 :(得分:1)
这确实等同。
但是......
didBid
也将由其他玩家设置(因为他们对该玩家持有相同的引用,覆盖每个玩家设置的内容。)
您正在创建一个可能需要手动中断的保留周期(或将对手属性设置为weak
)。
最好是每个玩家为每个对手保留一个“战略”对象,并且该策略将引用该玩家。这样,每个玩家可能会对任何其他玩家采取不同的策略。