拆分屏幕上Cocos2d [英] Split screen on Cocos2d

查看:170
本文介绍了拆分屏幕上Cocos2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个游戏,两个玩家在一个世界中相互对抗,但每个人都有自己的视口跟随他们的角色(分割屏幕),但我不知道如何实现它。这是为Cocos2d。



理想情况下,游戏事件将发生在自己的CCLayer中,代码只是将不同的部分绘制到不同的视口。然而,这似乎不可能与Cocos2d。



我想到的另一个解决方案是有两个不同的图层,并保持每个同步。这将工作,更容易实现视口代码,但似乎不可扩展。



我最后的想法是重写游戏层的绘制代码和手动使用OpenGL函数绘制它,但我不知道从哪里开始。



最好的方法是什么?

解决方案

好吧,经过一番研究,我发现此线程使用 glViewport 设置视口,然后调用 [super visit]



然而,这对我来说效果不好,因为某些原因,每个视口都有它的图像拉伸 - 我猜视口出了问题



我最终通过定位图层几乎完全使用Cocos2d函数重做视口,调用 [super visit] 并重新定位并再次调用 [super visit] 。通过与 glScissor 组合,我可以模仿视口



为了将来参考,这里是我使用的代码片段:

   - (void)visit 
{
glEnable(GL_SCISSOR_TEST);
glPushMatrix();

CGPoint point =((CCNode *)[toFollow objectAtIndex:1])。
self.anchorPoint = ccp(point.x / self.contentSize.width,point.y / self.contentSize.height);
self.rotation = 180.0f;

point =((CCNode *)[toFollow objectAtIndex:1])。
self.position = ccpAdd(ccp(bounds.width / 2,bounds.height / 4 * 3),ccp(-point.x,-point.y));
glScissor(0,bounds.height / 2,bounds.width,bounds.height / 2);
[super visit];

glPopMatrix();


glPushMatrix();
self.anchorPoint = CGPointZero;
self.rotation = 0.0f;

point =((CCNode *)[toFollow objectAtIndex:0])。

self.position = ccpAdd(ccp(bounds.width / 2,bounds.height / 4),ccp(-point.x,-point.y));
glScissor(0,0,bounds.width,bounds.height / 2);
[super visit];

glPopMatrix();
glDisable(GL_SCISSOR_TEST);
//self.rotation = 0.0f;
// [super visit];
}


I'm thinking of developing a game where two players play against each other in a "world" but each have their own viewport following their character (split screen) but I'm not sure how to implement it. This is for Cocos2d.

Ideally, the game events would occur in its own CCLayer and the code simply draws different sections of it onto different viewports. However, this doesn't seem to be possible with Cocos2d.

The other solution I thought about was having two different layers and keep each in sync. That would work and be easier to implement the viewport code but doesn't seem scalable.

The last idea I had was to override the draw code for the game layer and manually draw it using OpenGL functions but I wouldn't know where to start.

What would be the best way to do this?

解决方案

Alright, after a bit more research, I found this thread which uses glViewport to set up the viewports and then calling [super visit] for each viewport.

However, that didn't work well for me because for some reason, each viewport had its image stretched - I'm guessing the viewports had messed up something internal to Cocos2d.

I ended up redoing viewports almost completely using Cocos2d functions by positioning the layer, calling [super visit] and repositioning and calling [super visit] again. By combining this with glScissor, I can mimic viewports

For future reference, here's the snippet of code that I used:

-(void) visit
{
    glEnable(GL_SCISSOR_TEST);
    glPushMatrix();

    CGPoint point = ((CCNode*)[toFollow objectAtIndex:1]).position;
    self.anchorPoint = ccp(point.x/self.contentSize.width, point.y/self.contentSize.height);
    self.rotation = 180.0f;

    point = ((CCNode*)[toFollow objectAtIndex:1]).position;
    self.position = ccpAdd(ccp(bounds.width/2,bounds.height/4*3),ccp(-point.x, -point.y));
    glScissor(0,bounds.height/2,bounds.width,bounds.height/2);
    [super visit];

    glPopMatrix();


    glPushMatrix();
    self.anchorPoint = CGPointZero;
    self.rotation = 0.0f;

    point = ((CCNode*)[toFollow objectAtIndex:0]).position;

    self.position = ccpAdd(ccp(bounds.width/2,bounds.height/4),ccp(-point.x, -point.y));
    glScissor(0,0,bounds.width,bounds.height/2);
    [super visit];

    glPopMatrix();
    glDisable(GL_SCISSOR_TEST);
    //self.rotation = 0.0f;
    //[super visit];
}

这篇关于拆分屏幕上Cocos2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆