【发布时间】:2011-08-09 10:00:49
【问题描述】:
我正在使用 Cocos2D v0.99 在 iPhone 上制作太空入侵者克隆。我正在使用框架到达那里,但仍有很多我不知道。我坚持的步骤归结为碰撞。我需要做两件事:
1) 我需要与使用 spritesheet 动画的 CCSprite 舰队进行像素完美碰撞。 2)我需要与底座进行类似的像素完美碰撞,但我还需要为底座删除一些像素。
我找到了一些资源(http://groups.google.com/group/cocos2d-iphone-discuss/browse_thread/thread/f6e734e00d863f5e/41768952a1bcca0e?lnk=gst&q=image+mask)并尝试使用 CCMutableTexture2D 但是这个 cocos2d 版本不太好用。
我目前的方法是尝试修复 CCMutableTexture2D,但我正在努力将精灵纹理数据导入 MutableTexture。
这是执行此操作的最佳方法还是我缺少其他技术?
谢谢。
----- 更新 -----
我设法使用这里的颜色混合方法在模拟器中工作:http://www.cocos2d-iphone.org/forum/topic/18522
我还设法使用 CCRenderTexture 和混合函数通过 RenderMask 类(下面的 rm)进行像素删除。
但是...设备上的碰撞检测失败。我认为掩蔽工作正常。我最好的猜测是我正在使用的东西不是 OpenGL ES(或者我没有正确设置。我的代码是:
rt = [CCRenderTexture renderTextureWithWidth:480 height:320];
[rt beginWithClear:0 g:0 b:0 a:0];
[rm visit];
// Read pixels
ccColor4B *buffer = malloc( sizeof(ccColor4B) * numPixels );
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
[_rt end];
// loop through pixels
for(unsigned int i=0; i<numPixels; i++)
{
ccColor4B color = buffer[i];
if ((float)color.a >0) {
[self doSomeStuff];
}
}
那么,OpenGL ES 是否支持 glReadPixels()?或者,有没有更好的方法来做到这一点?
// RenderMask.h
//
// RenderMask.h
//
#import <UIKit/UIKit.h>
#import <stdlib.h>
#import "cocos2d.h"
@interface RenderMask : CCRenderTexture {
@public
NSMutableArray *sprites;
NSMutableArray *spriteMasks;
int currentSprite;
float frameInterval; //How fast this animates
}
-(void) initArrays;
-(void) drawCurrent;
-(void) addSpriteMask: (CCSprite*)spriteMask;
-(void) addSprite: (CCSprite*)sprite;
@property(nonatomic, retain) NSMutableArray *sprites;
@property(nonatomic, retain) NSMutableArray *spriteMasks;
@property(readwrite, assign) int currentSprite;
@property(readwrite, assign) float frameInterval;
@end
// RenderMask.m
//
// RenderMask.m
//
#import "RenderMask.h"
#import "CCActionInterval.h"
@implementation RenderMask
@synthesize sprites, spriteMasks, currentSprite, frameInterval;
-(void) initArrays {
sprites = [[NSMutableArray alloc] init];
spriteMasks = [[NSMutableArray alloc] init];
currentSprite = 0;
frameInterval = 0.1f;
}
-(void) drawCurrent {
[self clear:0.0f g:0.0f b:0.0f a:0.0f];
[self begin];
//Limit drawing to the alpha channel
NSValue *spriteValue = [sprites objectAtIndex:0];
CCSprite *sprite = (CCSprite*)[spriteValue pointerValue];
[sprite setOpacityModifyRGB:NO];
[sprite visit];
glColorMask(1.0f, 1.0f, 1.0f, 1.0f);
NSLog(@"spriteMasks size: %u", [spriteMasks count]);
for(NSValue *value in spriteMasks){
CCSprite *spriteMask = (CCSprite*)[value pointerValue];
[sprite setOpacityModifyRGB:NO];
[spriteMask visit];
}
glColorMask(1.0f, 1.0f, 1.0f, 1.0f);
[self end];
}
-(void) addSpriteMask: (CCSprite*)spriteMask {
NSLog(@"Adding spriteMask!!!");
[spriteMask retain];
[spriteMasks addObject:[NSValue valueWithPointer:spriteMask] ];
}
-(void) addSprite: (CCSprite*)sprite {
NSLog(@"Adding sprite!!!");
[sprite retain];
[sprites addObject:[NSValue valueWithPointer:sprite] ];
}
-(void) runAnimation {
NSLog(@"running animation");
[self drawCurrent];
currentSprite = currentSprite + 1;
if(currentSprite >= [sprites count]){
currentSprite = 0;
}
CCActionInterval * runAction = [CCCallFunc actionWithTarget:self selector:@selector(runAnimation)];
CCActionInterval * delayAndRunAction = [CCSequence actions:[CCDelayTime actionWithDuration:frameInterval], runAction, nil];
[self runAction:delayAndRunAction];
}
@end
此外,这段代码存在相当严重的性能问题,但我可能可以解决这个问题。
谢谢。
-- 更新(再次)------
我现在有一个部分工作的过程,其中数据结构基于基本初始化构建并用作碰撞查找。它有 50% 的时间在工作,但随后因 malloc EXC_BAD_ACCESS 错误而崩溃,直到我打开 Zombies 和 XCode 中的所有各种 malloc 调试选项。现在,我在生产线上遇到了一致的崩溃
CGContextClearRect(imageContext, CGRectMake(0, 0, contextWidth, contextHeight));
我通过这样做来达到这个目的(为了清楚起见,缺少几行):
CGImageRef image = [UIImage imageNamed:@"aspeaker.png"].CGImage;
imageContext = CGBitmapContextCreate(theData, width, height, 8, 4 * width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big);
NSLog(@"imageContext: %@",imageContext);
CGContextClearRect(imageContext, CGRectMake(0, 0, width, height));
CGContextTranslateCTM(imageContext, 0, height - imageSize.height);
CGContextDrawImage(imageContext, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
CGContextRelease(imageContext);
调试控制台显示:
2011-09-12 10:49:07.753 Invaders[1341:c803] imageContext: <CGContext 0x7f798fa0>
Invaders(1341,0xad06c2c0) malloc: protecting edges
Invaders(1341,0xad06c2c0) malloc: recording malloc stacks to disk using standard recorder
Invaders(1341,0xad06c2c0) malloc: enabling scribbling to detect mods to free blocks
我已经阅读了一些关于在正确线程上的内容,但看起来都在线程 1 上。
我们将非常感激地收到有关如何调试 malloc 错误的任何想法或指向良好资源的链接。
谢谢,
马丁
----- 另一个更新 -----
谷歌搜索出来的一件事是正确连接 OpenGL 上下文有一些复杂性(http://stackoverflow.com/questions/4459415/problem-with-opengles-to-show-an-image ) 我只是使用 cocos2d 0.99 基础所以这可能是问题吗?如果是这样,我该如何解决 *&%@£$?
【问题讨论】:
-
你见过this吗?
-
我确实看过一些 CGImage 类型的东西,但问题是如何从纹理获取任何类型的像素数据。 CCMutableTexture2D 似乎用 pixelAt 来做到这一点:但我不知道如何从 CCTexture2D 到 CCMutableTexture2D
标签: iphone opengl cocos2d-iphone