简体   繁体   English

iPhone上的简单2D游戏与cocos2d - 提示和技巧

[英]Simple 2D game on iPhone with cocos2d - tips and tricks

I'd like to create a simple 2d game: There is a car in the middle of the screen and the background is moving back resulting the feeling of the speed of the car. 我想创建一个简单的2D游戏:屏幕中间有一辆汽车,背景向后移动,产生了汽车速度的感觉。 The road has uphill and downhill as well so the car has to follow its shape. 这条路也有上坡和下坡,因此汽车必须遵循其形状。 The user can accelerate and break. 用户可以加速和打破。 Thats all! 就这样!

I did some research and found cocos2d. 我做了一些研究,发现了cocos2d。 I downloaded it, read the tutorials and studied the examples, but to start developing my simple game I still have questions I haven't got the answer. 我下载了它,阅读了教程并研究了这些例子,但是为了开始开发我的简单游戏,我仍然有问题我没有得到答案。

How should I create the road (2d) to put the car on it? 我应该如何创建将车放在上面的道路(2d)? How should I move the background (and the road) from right to left making the feel of the speed of the car? 我应该如何从右到左移动背景(和道路)来感受汽车的速度? How should I create the car? 我应该如何制造汽车?

I don't want to have source code as answer but theoretical advices, good tutorials and examples. 我不希望有源代码作为答案,但理论建议,良好的教程和示例。 Thanks!! 谢谢!!

I strongly reccomend you buy Steffen Itterheim's book "Learn Cocos2D" on Amazon . 我强烈建议你在亚马逊上购买Steffen Itterheim的书“Learn Cocos2D”。 It helped me a lot getting started by reading it and playing wit his source code: http://www.learn-cocos2d.com/ If you don't want to spend money look at RayWenderlich tutorials series. 它帮助我开始阅读它并使用他的源代码: http//www.learn-cocos2d.com/如果你不想花钱看看RayWenderlich教程系列。 There is a team working with him and there are lots of tutorials on how to build your first game (check them out, they are very good!): http://www.raywenderlich.com/tag/cocos2d . 有一个团队与他合作,有很多关于如何建立你的第一个游戏教程(检查出来,他们非常好!): http//www.raywenderlich.com/tag/cocos2d For moving a car is just a CCSprite object that moves during the update method. 移动汽车只是在更新方法期间移动的CCSprite对象。 Position is relative to abstract coordinates (0-320 width, and 0-640 height) and you can set it with the position property (sprite.position = CGPointMake(10.0f, 200.0f) will place your sprite in pos x:10, y:200). 位置是相对于抽象坐标(0-320宽度和0-640高度),您可以使用position属性设置它(sprite.position = CGPointMake(10.0f,200.0f)将您的精灵放在pos x:10中, Y:200)。 you need to add the sprite as child of the scene and scheduleUpdate in the init method and there have the logic to move the sprite according to the input (you can choose accellerometery input, or opt for some cool controllers like SneakyInput . More on this on Steffen's book :). 你需要在init方法中添加sprite作为场景的子节点和scheduleUpdate,并且有根据输入移动精灵的逻辑(你可以选择accellerometery输入,或者选择一些很酷的控制器,如SneakyInput 。更多关于这一点斯蒂芬的书:)。

You should add the "ParallaxBackground" as separate layer and get it to move by scheduling update. 您应该将“ParallaxBackground”添加为单独的图层,并通过安排更新来移动它。 Is the same concept as the car but with a different input (you don't need a controller for it but the input will be relative to the car's position. You can get the car as a child from the parent node by using parent.getChildByTag(CARTAG)). 是与汽车相同的概念,但具有不同的输入(您不需要控制器,但输入将相对于汽车的位置。您可以使用parent.getChildByTag从父节点获取汽车作为子项(CARTAG))。

This is a bit raugh but I hope to give an initial idea.. 这有点夸张,但我希望给出一个初步想法..

If you need a realistic result, you may consider using box2d or chipmunk (already embedded in cocos2d) to make the physics, so that the car will acts like those objects in "Doodle Truck" or "Tiny Wings". 如果你需要一个真实的结果,你可以考虑使用box2d或chipmunk(已经嵌入在cocos2d中)来制作物理,这样汽车就像“涂鸦卡车”或“小翅膀”中的那些物体一样。 There are pretty much tutorial on the Internet. 互联网上有很多教程。 Of cox, that require some more tuning and settings to make the game work properly. 对于考克斯来说,这需要更多的调整和设置才能使游戏正常运行。 Otherwise, you need to think about the data structure of the "road" - which make the car knows that it should be of which rotation angle. 否则,您需要考虑“道路”的数据结构 - 这使得汽车知道它应该是哪个旋转角度。 I think this is the most hard part of this simple game. 我认为这是这个简单游戏中最难的部分。

For moving the background, that is simple and should have many possible ways. 为了移动背景,这很简单,应该有很多可能的方法。 The simplest I can think of is setting its position frame by frame according to the current speed of the car. 我能想到的最简单的方法是根据汽车的当前速度逐帧设置其位置。

Creating the car: you can make a class representing it, including a CCSprite in it, which handle the animations, rotations, flip....and anything else related to displaying the car. 创建汽车:你可以创建一个代表它的类,包括一个CCSprite,它可以处理动画,旋转,翻转....以及与显示汽车相关的任何其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM