2017-09-11 4 views
1

Hey je développe une application Cocos2dx avec XCode et iPhone 5c et je cherche à changer le système de coordonnées en portrait. J'ai regardé ces directions http://www.cocos2d-x.org/wiki/Device_Orientation. Selon la direction, vous devez changer quelques méthodes dans le RootViewController afin qu'avant que Cocos ne fasse sa magie, iOS a déjà géré la rotation du conteneur.Cocos2dx v.3.0 Mode portrait

Maintenant, les méthodes applicables de mon RootViewController.mm sont les suivantes. Il construit et fonctionne.

#ifdef __IPHONE_6_0 
- (NSUInteger) supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait; 
} 
#endif 

- (BOOL) shouldAutorotate { 
    return YES; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return UIInterfaceOrientationIsPortrait (interfaceOrientation); 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; 

    auto glview = cocos2d::Director::getInstance()->getOpenGLView(); 

    if (glview) 
    { 
     CCEAGLView *eaglview = (__bridge CCEAGLView *)glview->getEAGLView(); 

     if (eaglview) 
     { 
      CGSize s = CGSizeMake([eaglview getWidth], [eaglview getHeight]); 
      cocos2d::Application::getInstance()->applicationScreenSizeChanged((int) s.width, (int) s.height); 
     } 
    } 
} 
@end 

Le code de la méthode de mon niveau ressemble à ceci

bool Level1::init() 
{ 
    ////////////////////////////// 
    // 1. super init first 
    if (!Scene::init()) 
    { 
     return false; 
    } 

    auto visibleSize = Director::getInstance()->getVisibleSize(); 
    Vec2 origin = Director::getInstance()->getVisibleOrigin(); 

    for(int i= 0; i < MAX_TOUCHES; ++i) { 
     labelTouchLocations[i] = Label::createWithSystemFont("", "Arial", 42); 
     labelTouchLocations[i]->setVisible(false); 
     this->addChild(labelTouchLocations[i]); 
    } 

    auto eventListener = EventListenerTouchAllAtOnce::create(); 

    // Create an eventListener to handle multiple touches, using a lambda, cause baby, it's C++11 
    eventListener->onTouchesBegan = [=](const std::vector<Touch*>&touches, Event* event){ 

     // Clear all visible touches just in case there are less fingers touching than last time 
     std::for_each(labelTouchLocations,labelTouchLocations+MAX_TOUCHES,[](Label* touchLabel){ 
      touchLabel->setVisible(false); 
     }); 

     // For each touch in the touches vector, set a Label to display at it's location and make it visible 
     for(int i = 0; i < touches.size(); ++i){ 
      labelTouchLocations[i]->setPosition(touches[i]->getLocation()); 
      labelTouchLocations[i]->setVisible(true); 
      labelTouchLocations[i]->setString("Touched"); 
      std::cout << "(" << touches[i]->getLocation().x << "," << touches[i]->getLocation().y << ")" << std::endl; 

     } 
    }; 

    _eventDispatcher->addEventListenerWithSceneGraphPriority(eventListener, this); 

    return true; 
} 

init() Quand je touche le point le plus bas partie gauche de l'iPhone, la coordonnée x du point de contact I l'impression est d'environ 150. La coordonnée y est 0, comme prévu. Pourquoi la coordonnée x n'est pas nulle? Y a-t-il quelque chose qui me manque dans la création de la scène? Je crois que j'ai fait tous les changements requis par la documentation de Cocos. Leurs documents sont-ils périmés?

Répondre

1

Le système de coordonnées ne démarre pas à (0,0) en bas à gauche comme le suggère la documentation. Cependant, il existe des méthodes simples qui peuvent être utilisées pour obtenir l'origine et la taille du conteneur inclus dans la scène par défaut.

auto visibleSize = Director::getInstance()->getVisibleSize(); 
Vec2 origin = Director::getInstance()->getVisibleOrigin();