- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在我的游戏中,玩家可以激活网格中的对象(这里是方 block )。在示例图片中,假设所有红色方 block 都已“激活”。当玩家触摸任何红色方 block (例如带有紫色箭头的方 block )时,我想向所有汇合的相邻红色方 block 以及任何被红色方 block (绿色方 block )绑定(bind)的方 block 发送消息。
我不介意能够将消息发送到也被红色瓷砖或网格墙绑定(bind)的瓷砖(如果只有一堵墙,不包括角落或整个空白空间)。我在这里将其显示为蓝色瓷砖。我也不介意在红色方 block 呈对角线而不是相邻(黄色形状)时尝试让它发挥作用,但我不确定其中任何一个是否会进入游戏玩法。
这是一种简化,但我只需要一些关于去哪里的建议。我只需要知道我需要做的数学类型。
不确定它是否需要帮助,但游戏是在 Objective-C 和 Cocos2D 中,每个图 block 都是子类对象,并且有一个属性来告诉它是哪个状态,并且在数组的数组中。
这篇文章似乎不相关:surrounding objects algorithm
最佳答案
所以,这并不完美,但它确实有效。我不会详细介绍,但我使用了 learncocos2d/steffen 的方法,然后使用了我自己的边界检测器版本来尝试检测漏洞。它仅在水平扫描中执行此操作。显然,该代码包括对一组框的调用,我没有为此进行更改,但正如问题中所述,arrayofboxes 是一个数组,其中每个条目都是一行名为 Box 的图 block :
我发布这个以防它对任何人有帮助,但我还不能 100% 确定没有错误
-(NSMutableDictionary*) returnMooreNeighborDictFromBox:(Box*)enteredBox {
NSMutableDictionary * mooreDictToReturn = [NSMutableDictionary dictionaryWithCapacity:8];
//define moore-neighbor as the 8 boxes around any given box. p1 is upper left
/*
P1 P2 P3
P8 XX P4
P7 P6 P5
*/
int depth = enteredBox.trueDepth;
int depthRelativeXCoord = enteredBox.gridAbsoluteXCoord/((pow(2,depth)));
int depthRelativeYCoord = enteredBox.gridAbsoluteYCoord/((pow(2,depth)));
int xPosInArray = depthRelativeXCoord;
int yPosInArray = depthRelativeYCoord;
int maxCoord = (self.numberOfBoxesOfAcross/(pow(2,depth))-1);
NSMutableArray *boxArray = [arrayOfBoxArrays objectAtIndex:depth];
NSMutableArray *boxRowOfArray = [boxArray objectAtIndex:yPosInArray];
if ((xPosInArray-1)>=0){
Box * p8Box = [boxRowOfArray objectAtIndex:xPosInArray-1];
[mooreDictToReturn setObject:p8Box forKey:@"p8"];
}
else {
[mooreDictToReturn setObject:[NSNull new] forKey:@"p8"];
}
if ((xPosInArray+1)<=maxCoord){
Box * p4Box = [boxRowOfArray objectAtIndex:xPosInArray+1];
[mooreDictToReturn setObject:p4Box forKey:@"p4"];
}
else {
[mooreDictToReturn setObject:[NSNull new] forKey:@"p4"];
}
if ((yPosInArray-1)>=0){
NSMutableArray *boxRowBelowArray = [boxArray objectAtIndex:yPosInArray-1];
Box * p6Box = [boxRowBelowArray objectAtIndex:xPosInArray];
[mooreDictToReturn setObject:p6Box forKey:@"p6"];
if ((xPosInArray-1)>=0){
Box * p7Box = [boxRowBelowArray objectAtIndex:xPosInArray-1];
[mooreDictToReturn setObject:p7Box forKey:@"p7"];
}
else {
[mooreDictToReturn setObject:[NSNull new] forKey:@"p7"];
}
if ((xPosInArray+1)<=maxCoord){
Box * p5Box = [boxRowBelowArray objectAtIndex:xPosInArray+1];
[mooreDictToReturn setObject:p5Box forKey:@"p5"];
}
else {
[mooreDictToReturn setObject:[NSNull new] forKey:@"p5"];
}
}
else {
[mooreDictToReturn setObject:[NSNull new] forKey:@"p5"];
[mooreDictToReturn setObject:[NSNull new] forKey:@"p6"];
[mooreDictToReturn setObject:[NSNull new] forKey:@"p7"];
}
if ((yPosInArray+1)<=maxCoord){
NSMutableArray *boxRowAboveArray = [boxArray objectAtIndex:yPosInArray+1];
Box * p2Box = [boxRowAboveArray objectAtIndex:xPosInArray];
[mooreDictToReturn setObject:p2Box forKey:@"p2"];
if ((xPosInArray-1)>=0){
Box * p1Box = [boxRowAboveArray objectAtIndex:xPosInArray-1];
[mooreDictToReturn setObject:p1Box forKey:@"p1"];
}
else {
[mooreDictToReturn setObject:[NSNull new] forKey:@"p1"];
}
if ((xPosInArray+1)<=maxCoord){
Box * p3Box = [boxRowAboveArray objectAtIndex:xPosInArray+1];
[mooreDictToReturn setObject:p3Box forKey:@"p3"];
}
else {
[mooreDictToReturn setObject:[NSNull new] forKey:@"p3"];
}
}
else {
[mooreDictToReturn setObject:[NSNull new] forKey:@"p1"];
[mooreDictToReturn setObject:[NSNull new] forKey:@"p2"];
[mooreDictToReturn setObject:[NSNull new] forKey:@"p3"];
}
return mooreDictToReturn;
-(NSString*) stepClockwiseMooreItem:(NSString*) currentMooreItem {
CCLOG(@"STEPCLOC");
//go through the 8-moore clockwise starting at P6 until hit. add hit to array. mark "entry box" as last box that was inactive before hit
//go to hit box and go through 8 starting with last hit box (if p3 was prior hit then p2 was empty so go to p8 of new box. these are the pairs: if X hit then prior empty was Y: p1>p8 P2>p1, P3>p2, P4>p3 etc.
NSString *returnString;
if ([currentMooreItem isEqualToString:@"p1"]){returnString= [NSString stringWithFormat:@"p2"];}
else if ([currentMooreItem isEqualToString:@"p2"]){returnString= [NSString stringWithFormat:@"p3"];}
else if ([currentMooreItem isEqualToString:@"p3"]){returnString= [NSString stringWithFormat:@"p4"];}
else if ([currentMooreItem isEqualToString:@"p4"]){returnString= [NSString stringWithFormat:@"p5"];}
else if ([currentMooreItem isEqualToString:@"p5"]){returnString= [NSString stringWithFormat:@"p6"];}
else if ([currentMooreItem isEqualToString:@"p6"]){returnString= [NSString stringWithFormat:@"p7"];}
else if ([currentMooreItem isEqualToString:@"p7"]){returnString= [NSString stringWithFormat:@"p8"];}
else if ([currentMooreItem isEqualToString:@"p8"]){returnString= [NSString stringWithFormat:@"p1"];}
return returnString;
-(NSString*) stepCounterClockMooreItem:(NSString*) currentMooreItem {
//go through the 8-moore clockwise starting at P6 until hit. add hit to array. mark "entry box" as last box that was inactive before hit
//go to hit box and go through 8 starting with last hit box (if p3 was prior hit then p2 was empty so go to p8 of new box. these are the pairs: if X hit then prior empty was Y: p1>p8 P2>p1, P3>p2, P4>p3 etc.
NSString *returnString;
if ([currentMooreItem isEqualToString:@"p1"]){returnString= [NSString stringWithFormat:@"p8"];}
else if ([currentMooreItem isEqualToString:@"p2"]){returnString= [NSString stringWithFormat:@"p1"];}
else if ([currentMooreItem isEqualToString:@"p3"]){returnString= [NSString stringWithFormat:@"p2"];}
else if ([currentMooreItem isEqualToString:@"p4"]){returnString= [NSString stringWithFormat:@"p3"];}
else if ([currentMooreItem isEqualToString:@"p5"]){returnString= [NSString stringWithFormat:@"p4"];}
else if ([currentMooreItem isEqualToString:@"p6"]){returnString= [NSString stringWithFormat:@"p5"];}
else if ([currentMooreItem isEqualToString:@"p7"]){returnString= [NSString stringWithFormat:@"p6"];}
else if ([currentMooreItem isEqualToString:@"p8"]){returnString= [NSString stringWithFormat:@"p7"];}
return returnString;
}
-(NSString*) stepMooreBacktrackAfterHitAt:(NSString*) currentMooreItem {
// with the moore, you can only back track adjacenetly, not diagnally, so its not straightforward counterclockwise walk
NSString *returnString;
if ([currentMooreItem isEqualToString:@"p1"]){returnString= [NSString stringWithFormat:@"p6"];}
else if ([currentMooreItem isEqualToString:@"p2"]){returnString= [NSString stringWithFormat:@"p8"];}
else if ([currentMooreItem isEqualToString:@"p3"]){returnString= [NSString stringWithFormat:@"p8"];}
else if ([currentMooreItem isEqualToString:@"p4"]){returnString= [NSString stringWithFormat:@"p2"];}
else if ([currentMooreItem isEqualToString:@"p5"]){returnString= [NSString stringWithFormat:@"p2"];}
else if ([currentMooreItem isEqualToString:@"p6"]){returnString= [NSString stringWithFormat:@"p4"];}
else if ([currentMooreItem isEqualToString:@"p7"]){returnString= [NSString stringWithFormat:@"p4"];}
else if ([currentMooreItem isEqualToString:@"p8"]){returnString= [NSString stringWithFormat:@"p6"];}
return returnString;
-(void) boxAreaCollapse:(float)boxCenterGridCoordX andY:(float)boxCenterGridCoordY atDepth:(int)depth{
//ok, to do this, we'll use moore neighbor tracing
//might be able to capture edges by add a row above and below and to either side of grid. each of these has a unique identity so if end wall included or something but this is later
//set array of boxes to empty
NSMutableArray * boxesOnEdgeOfShapeContainingTouchedBox = [NSMutableArray arrayWithCapacity:( (self.numberOfBoxesOfAcross/(pow(2,depth)))*(self.numberOfBoxesOfAcross/(pow(2,depth))) )];
//get array of array for this "depth"
NSMutableArray *boxArray = [arrayOfBoxArrays objectAtIndex:depth];
//get the box that was touched
NSMutableArray * boxRowSameAsTouchedBox = [boxArray objectAtIndex:boxCenterGridCoordY];
Box * boxTouched = [boxRowSameAsTouchedBox objectAtIndex:boxCenterGridCoordX];
CCLOG(@"boxTouched %i,%i",boxTouched.gridAbsoluteXCoord,boxTouched.gridAbsoluteYCoord);
//define boundaries > dont need to do this
//bottom left coord is (0,0), top right is (maxCoord,maxCoord)
//float maxCoord = (self.numberOfBoxesOfAcross/(pow(2,depth))-1); // the -1 because the first is "coord" is 0
//must start on bottom left so move all the way left until hit inactive then down
//move to the left through adjacenet boxes from touched best to find the furthest left connected directly to touch box
int numberOfBoxesLeft = boxCenterGridCoordX;
Box * furthestLeftBox = boxTouched;
for (int i = 1; i <= numberOfBoxesLeft; i++){
Box * box = [boxRowSameAsTouchedBox objectAtIndex:boxCenterGridCoordX-i];
if (box.boxState == kBoxActivated || box.boxState == kBoxClicked || box.boxState == kBoxLastDepthCompleted){
furthestLeftBox = box;
}
else {
break;
}
}
//move down from there to connected boxes below
int furthestLeftXCoord = (furthestLeftBox.gridAbsoluteXCoord / ((pow(2,depth))));
int furthestLeftYCoord = (furthestLeftBox.gridAbsoluteYCoord / ((pow(2,depth))));
int numberOfBoxesBelow = furthestLeftYCoord;
Box * furthestDownBox = furthestLeftBox;
for (int i = 1; i <= numberOfBoxesBelow; i++){
NSMutableArray * boxRowBelow = [boxArray objectAtIndex:furthestLeftYCoord-i];
Box * box = [boxRowBelow objectAtIndex:furthestLeftXCoord];
if (box.boxState == kBoxActivated || box.boxState == kBoxClicked || box.boxState == kBoxLastDepthCompleted){
furthestDownBox = box;
}
else {
break;
}
}
//define starting Box and "entry box" for defining when to STOP this algorithim. we know p6 from the starting box should be empty and when we reenter the starting box from p6 we will define the "end"
Box * startingBoxFilled = furthestDownBox;
NSString * startingEnterPoint = @"p6";
//add start box to shape array > dont need to do this, it'll happen in first load of loop
//[boxesOnEdgeOfShapeContainingTouchedBox addObject:furthestDownBox];
//go through the 8-moore clockwise starting at P6 until hit. add hit to array. mark "entry box" as last box that was inactive before hit
//go to hit box and go through 8 starting with last hit box (if p3 was prior hit then p2 was empty so go to p8 of new box. these are the pairs: if X hit then prior empty was Y: p1>p8 P2>p1, P3>p2, P4>p3 etc.
//define first hitBox and entry point for backtrack
Box * hitBox =furthestDownBox;
NSString * hitBoxEnteredFrom = @"p6";
//CCLOG(@"fursthest down box %i,%i: %@, ",hitBox.gridAbsoluteXCoord, hitBox.gridAbsoluteYCoord,hitBox);
BOOL algorithimRunning = YES;
BOOL singleCircleRunning;
while (algorithimRunning == YES) {
//CCLOG(@"starting loop for box relX %i, relY%i", hitBox.gridAbsoluteXCoord, hitBox.gridAbsoluteYCoord);
NSMutableDictionary * mooreDictForBox = [self returnMooreNeighborDictFromBox:hitBox];
singleCircleRunning = YES;
NSString * circleStartingChecker = hitBoxEnteredFrom;
while (singleCircleRunning == YES){
//CCLOG(@"inner circle loop from box relX %i, relY%i checking %@ ", hitBox.gridAbsoluteXCoord, hitBox.gridAbsoluteYCoord,hitBoxEnteredFrom);
if ([hitBoxEnteredFrom isEqualToString:circleStartingChecker]){
singleCircleRunning = NO;
//CCLOG(@"loop circled around, ending inner loop");
}
if ([[mooreDictForBox objectForKey:hitBoxEnteredFrom] isKindOfClass:[Box class]]){
Box * box = [mooreDictForBox objectForKey:hitBoxEnteredFrom];
//CCLOG(@"moore object is box: state %i, depth %i, gridX %i, y %i", box.boxState, box.trueDepth, box.gridAbsoluteXCoord, box.gridAbsoluteYCoord);
if (box.boxState == kBoxActivated || box.boxState == kBoxClicked || box.boxState == kBoxLastDepthCompleted){
hitBox = box;
[boxesOnEdgeOfShapeContainingTouchedBox addObject:box];
NSString * newmooreChecker = [self stepMooreBacktrackAfterHitAt:hitBoxEnteredFrom];
hitBoxEnteredFrom = newmooreChecker;
singleCircleRunning = NO;
//CCLOG(@"box is activated, etc, adjusting to %@ for backtrack ", hitBoxEnteredFrom);
}
else {
NSString * newmooreChecker = [self stepClockwiseMooreItem:hitBoxEnteredFrom];
hitBoxEnteredFrom = newmooreChecker;
//CCLOG(@"box is not activated, etc, check next moore");
}
}
else {
//CCLOG(@"moore object is nil, check next moore object");
NSString * newmooreChecker = [self stepClockwiseMooreItem:hitBoxEnteredFrom];
hitBoxEnteredFrom = newmooreChecker;
}
}
//repeat until you reach "starting down". if entered starting down from "entry" stop.
if ([hitBox isEqual:startingBoxFilled] && [hitBoxEnteredFrom isEqualToString:startingEnterPoint]) {
algorithimRunning = NO;
//CCLOG(@"hitbox is starting box and entered from is same as start");
}
}
//create an array of dictionaries to represent "on" and "off" for the rows.
//first initialize the array of row arrays with NO in each pixel slot
int numberOfRows = (self.numberOfBoxesOfAcross/(pow(2,depth)));
NSMutableArray * arrayOfRowsForShape = [NSMutableArray arrayWithCapacity:numberOfRows];
for (int i= 0; i< numberOfRows; i++){
NSMutableDictionary * rowDict = [NSMutableDictionary dictionaryWithCapacity:numberOfRows];
for (int p = 0; p< numberOfRows; p++){
[rowDict setObject:[NSString stringWithFormat:@"OUTSIDE"] forKey:[NSNumber numberWithInt:p]];
}
[arrayOfRowsForShape addObject:rowDict];
}
//go through boxes in shape and change to YES if box located at that spot
for (Box * box in boxesOnEdgeOfShapeContainingTouchedBox) {
NSMutableDictionary * rowDict = [arrayOfRowsForShape objectAtIndex:box.gridAbsoluteYCoord/(pow(2,depth))];
[rowDict setObject:[NSString stringWithFormat:@"EDGE"] forKey:[NSNumber numberWithInt:box.gridAbsoluteXCoord/(pow(2,depth))]];
}
//go through array of dict and for each one, go left to right and mark anything bound on both sides as "inside"
for (int locationY = 0; locationY<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationY++ ){
NSMutableDictionary * rowDict = [arrayOfRowsForShape objectAtIndex:locationY];
BOOL possiblyInsideContour = NO;
BOOL edgeFound = NO;
int holesFound = 0;
for (int locationXin = 0; locationXin<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationXin++ ){
NSNumber * locationX = [NSNumber numberWithInt:locationXin];
NSMutableArray * markForChanging = [NSMutableArray arrayWithCapacity:(self.numberOfBoxesOfAcross/(pow(2,depth)))];
NSString * thisLocation = [rowDict objectForKey:locationX];
if ([thisLocation isEqualToString:@"EDGE"] && edgeFound==NO && possiblyInsideContour==NO ){
edgeFound=YES;
}
else if ([thisLocation isEqualToString:@"OUTSIDE"] && edgeFound==YES && possiblyInsideContour==NO){
possiblyInsideContour=YES;
[rowDict setObject:@"MAYBEINSIDE" forKey:locationX];
[markForChanging addObject:[rowDict objectForKey:locationX]];
}
else if ([thisLocation isEqualToString:@"OUTSIDE"] && possiblyInsideContour==YES ){
[rowDict setObject:@"MAYBEINSIDE" forKey:locationX];
edgeFound=NO;
}
else if ([thisLocation isEqualToString:@"EDGE"] && possiblyInsideContour==YES ){
possiblyInsideContour=NO;
holesFound++;
edgeFound=YES;
}
}
int holesPatched = 0;
BOOL patchingHole=NO;
while (holesPatched < holesFound){
for (int locationXin = 0; locationXin<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationXin++ ){
NSNumber * locationX = [NSNumber numberWithInt:locationXin];
NSString * thisLocation = [rowDict objectForKey:locationX];
if ([thisLocation isEqualToString:@"MAYBEINSIDE"]){
patchingHole = YES;
[rowDict setObject:@"INSIDE" forKey:locationX];
}
else if ([thisLocation isEqualToString:@"EDGE"] && patchingHole == YES){
holesPatched++;
patchingHole=NO;
}
}
}
}
int locationY=0;
for (NSMutableDictionary *rowDict in arrayOfRowsForShape){
for (int locationXin = 0; locationXin<(self.numberOfBoxesOfAcross/(pow(2,depth))); locationXin++ ){
NSNumber * locationX = [NSNumber numberWithInt:locationXin];
NSString * thisLocation = [rowDict objectForKey:locationX];
if ([thisLocation isEqualToString:@"INSIDE"]){
CCLOG(@"inside at %i,%i", [locationX intValue], locationY);
}
}
locationY++;
}
}
关于algorithm - 确定网格上的一个点是否被某种类型的点包围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18166288/
我正在尝试编写一个相当多态的库。我遇到了一种更容易表现出来却很难说出来的情况。它看起来有点像这样: {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE
谁能解释一下这个表达式是如何工作的? type = type || 'any'; 这是否意味着如果类型未定义则使用“任意”? 最佳答案 如果 type 为“falsy”(即 false,或 undef
我有一个界面,在IAnimal.fs中, namespace Kingdom type IAnimal = abstract member Eat : Food -> unit 以及另一个成功
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: What is the difference between (type)value and type(va
在 C# 中,default(Nullable) 之间有区别吗? (或 default(long?) )和 default(long) ? Long只是一个例子,它可以是任何其他struct类型。 最
假设我有一个案例类: case class Foo(num: Int, str: String, bool: Boolean) 现在我还有一个简单的包装器: sealed trait Wrapper[
这个问题在这里已经有了答案: Create C# delegate type with ref parameter at runtime (1 个回答) 关闭 2 年前。 为了即时创建委托(dele
我正在尝试获取图像的 dct。一开始我遇到了错误 The function/feature is not implemented (Odd-size DCT's are not implemented
我正在尝试使用 AFNetworking 的 AFPropertyListRequestOperation,但是当我尝试下载它时,出现错误 预期的内容类型{( “应用程序/x-plist” )}, 得
我在下面收到错误。我知道这段代码的意思,但我不知道界面应该是什么样子: Element implicitly has an 'any' type because index expression is
我尝试将 SignalType 从 ReactiveCocoa 扩展为自定义 ErrorType,代码如下所示 enum MyError: ErrorType { // .. cases }
我无法在任何其他问题中找到答案。假设我有一个抽象父类(super class) Abstract0,它有两个子类 Concrete1 和 Concrete1。我希望能够在 Abstract0 中定义类
我想知道为什么这个索引没有用在 RANGE 类型中,而是用在 INDEX 中: 索引: CREATE INDEX myindex ON orders(order_date); 查询: EXPLAIN
我正在使用 RxJava,现在我尝试通过提供 lambda 来订阅可观察对象: observableProvider.stringForKey(CURRENT_DELETED_ID) .sub
我已经尝试了几乎所有解决问题的方法,其中包括。为 提供类型使用app.use(express.static('public'))还有更多,但我似乎无法为此找到解决方案。 index.js : imp
以下哪个 CSS 选择器更快? input[type="submit"] { /* styles */ } 或 [type="submit"] { /* styles */ } 只是好
我不知道这个设置有什么问题,我在 IDEA 中获得了所有注释(@Controller、@Repository、@Service),它在行号左侧显示 bean,然后转到该 bean。 这是错误: 14-
我听从了建议 registering java function as a callback in C function并且可以使用“简单”类型(例如整数和字符串)进行回调,例如: jstring j
有一些 java 类,加载到 Oracle 数据库(版本 11g)和 pl/sql 函数包装器: create or replace function getDataFromJava( in_uLis
我已经从 David Walsh 的 css 动画回调中获取代码并将其修改为 TypeScript。但是,我收到一个错误,我不知道为什么: interface IBrowserPrefix { [
我是一名优秀的程序员,十分优秀!