没有接触触摸!

时间:2010-12-14 08:12:53

标签: iphone opengl-es touch game-loop

好的,我猜我之前的问题并不详细。 我正在提供更多细节。

在发布完整源代码之前,我不得不问我的问题: 我的问题是为什么以下程序不会调用touchesBegan,touchesMoved和touchesEnded?它曾用于以前的iOS SDK。我目前的xcode版本是3.2.5

一条评论:如果你在iPad模拟器上运行它,下面的代码会调用所有触摸。因此,iPhone模拟器和实际设备无法解决问题的原因非常奇怪。

  

的main.m

#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
    [pool release];
    return retVal;
}

  

AppDelegate.h

#import <UIKit/UIKit.h>
@class GLView;

@interface AppDelegate : NSObject <UIApplicationDelegate> 
{
    UIWindow*   mp_window;
    GLView*     mp_viewController;
}

@end

  

AppDelegate.mm

#import "AppDelegate.h"
#import "GLView.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    CGRect screenBounds = [[UIScreen mainScreen] bounds];

    mp_window = [[UIWindow alloc] initWithFrame: screenBounds];
    mp_viewController = [[GLView alloc] initWithFrame: screenBounds];   


    [mp_window addSubview: mp_viewController];
    [mp_window makeKeyAndVisible];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{}

- (void)applicationDidBecomeActive:(UIApplication *)application
{}

- (void)applicationWillTerminate:(UIApplication *)application
{}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Handle any background procedures not related to animation here.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Handle any foreground procedures not related to animation here.
}

- (void)dealloc
{
    [mp_viewController release];
    [mp_window release];

    [super dealloc];
}

@end

  

GLView.h

#import <OpenGLES/EAGL.h>
#import <QuartzCore/QuartzCore.h>

@interface GLView : UIView <UIAccelerometerDelegate>
{
@private
    EAGLContext*        mp_context;
}

- (void) gameLoop;

- (void) drawView: (float) interpolation;

- (void)touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event;
- (void)touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event;
- (void)touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event;

- (void)accelerometer: (UIAccelerometer *) accelerometer 
        didAccelerate: (UIAcceleration *) acceleration;

@end

  

GLView.mm

#import "GLView.h"
#import <OpenGLES/ES2/gl.h>

@implementation GLView

+ (Class) layerClass
{
    return [CAEAGLLayer class];
}

- (id) initWithFrame: (CGRect) frame
{
    if (self = [super initWithFrame: frame]) 
    {
        CAEAGLLayer* eaglLayer = (CAEAGLLayer*) super.layer; 
        eaglLayer.opaque = YES; 

        EAGLRenderingAPI api = kEAGLRenderingAPIOpenGLES1;
        mp_context = [[EAGLContext alloc] initWithAPI: api];

        if (!mp_context || ![EAGLContext setCurrentContext: mp_context]) 
        {
            [self release];
            return nil;
        }

        [mp_context renderbufferStorage: GL_RENDERBUFFER 
                           fromDrawable: eaglLayer];

        //TODO: to setup the size of frame for render
        //////

        //acc
        [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(0.1f)];
        [[UIAccelerometer sharedAccelerometer] setDelegate:self];

        //multi - touch
        [self setUserInteractionEnabled:YES];
        [self setMultipleTouchEnabled:YES];


        [self performSelectorOnMainThread:@selector(gameLoop) 
                               withObject:nil waitUntilDone:NO];
    }

    return self;
}

- (void) gameLoop
{   
    while(1)
    {
            //Get all touches
        while(CFRunLoopRunInMode(kCFRunLoopDefaultMode, 
                     0.002f, 
                     TRUE) == kCFRunLoopRunHandledSource);
            //render scene
            [drawView 1.0f];
    }
}

- (void) drawView: (float) interpolation
{
    //TODO: adding render image here
    //NSLog(@"Render: %f", interpolation);
    [mp_context presentRenderbuffer: GL_RENDERBUFFER];
}

- (void) dealloc
{

    if ([EAGLContext currentContext] == mp_context)
        [EAGLContext setCurrentContext: nil];


    [mp_context release]; 
    //TODO: relese all objects here
    [super dealloc];    
}

- (void)touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event 
{
    int hash;
    CGPoint points;
    for(UITouch * touch in touches)
    {
        hash = [touch hash];
        points = [touch locationInView: self];
        NSLog(@"Touch Began: %d, %f, %f", hash, points.x, points.y);
    }
}


- (void)touchesMoved: (NSSet*) touches withEvent: (UIEvent*) event 
{
    int hash;
    CGPoint points;
    for(UITouch * touch in touches)
    {
        hash = [touch hash];
        points = [touch locationInView: self];
        NSLog(@"Touch Moved: %d, %f, %f", hash, points.x, points.y);
    }
}


- (void)touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event 
{
    int hash;
    CGPoint points;
    for(UITouch * touch in touches)
    {
        hash = [touch hash];
        points = [touch locationInView: self];
        NSLog(@"Touch Ended: %d, %f, %f", hash, points.x, points.y);
    }
}

- (void) accelerometer: (UIAccelerometer *) accelerometer 
         didAccelerate: (UIAcceleration *) acceleration 
{
    NSLog(@"Accelerometer: (%f, %f, %f)", 
          acceleration.x, 
          acceleration.y, 
          acceleration.z );
}

@end

这只是一个测试案例。我主要担心的是为什么使用新的SDK,不会调用触摸?请不要告诉我必须使用NSTimer或其他计时器。我在安装新SDK之前就已经完成了工作。我只是想知道是否有人可以看到除此之外的源代码中的任何问题。

谢谢, 孟菲斯

1 个答案:

答案 0 :(得分:0)

我不确定你的问题是什么。但是,我注意到你没有视图控制器 - 而你的UIApplicationDelegate子类有一个mp_viewController成员,它是一个UIView而不是UIViewController。这可能是您问题的根源。

我建议添加一个与您的视图关联的UIViewController,并将其分配给应用程序委托的rootViewController属性。