自学cocos2d js游戏开发应该按什么步骤进行

JavaScript031

自学cocos2d js游戏开发应该按什么步骤进行,第1张

跟你简要说一下我的自学路,以免你走弯路

背景:

有工作,而且很忙;在不影响工作基础上用业余时间鼓捣(经常会因为工作耽搁学习进度);基本没技术背景(初中qbasic、高中pascal半吊子),大学文科,工作也不是搞代码。因此我感觉我的自学路跟你还是挺契合的。

1

两年前的一天,决定要做独立游戏制作人。选定引擎cocos2d,开始学习objective-c语言;(如果你打算只在iOS平台开发可以学obj-c配合cocos2d或者sprite kit;如果打算跨平台就要用cocos2d-x和c++了;当然unity什么的也不错但是我还没尝试过,一直很向往)

2

我跳过了iOS程序开发(似懂非懂看完了斯坦福那一系列的教学视频),结合当时情况直接选择了cocos2d引擎,这时开始接触 @吴一刀 推荐的博客;我主要看的是子龙山人<子龙山人 - 博客园>、Ray Wenderlich<Ray Wenderlich>、Himi<黑米GameDev街区>这些博客里最基本的例子,知易那个以我当时的水平还看不懂。

3

尝试开始设计自己的简单游戏,我做打地鼠!当然比教学例子里的打地鼠复杂多了

4

这个过程中发现自己懂的实在是不够,所以又开啃这本书:Learn cocos2d 2 by Steffen Itterheim &Andreas Low

5

慢慢的发现需要一些趁手的工具和编辑器,并发展出一套自己构建于cocos2d之上的游戏设计架构。我目前的情况是这样:

关卡编辑我使用plist文件配合自己写的类;

sprite sheet使用TexturePacker<TexturePacker>;

动画/UI编辑最初自己手写太累,后来选择了cocosbuilder,请注意现在这个软件已经停止维护,转而引导用户使用SpriteBuilder<SpriteBuilder>(域名都做了自动跳转;SpriteBuilder我木有研究过,建议你研究一下)。

粒子编辑我自己还没实际用到,如果真正需要我感觉cocosbuilder的够我用的。//更新:后来用到了ParticleDesigner和cocosbuilder两个软件的粒子编辑混搭

我当时遇到的一个大麻烦是如何让cocosbuilder、cocos2d版本互相兼容且cocosbuilder无问题。大概情况是这样:cocosbuilder2.1和cocos2d-iphone2.0及2.1版本都不协调怎么办?;What version of Cocosbuilder and Cocos2d-iphone should I choose?

目前我使用的cocosbuilder 2.1版本(渐变层有bug、并且很可惜动画播放完没有触发消息的机制,我只好把每个动画时长都手动记录一下自己处理)

======3月12日更新======

在 @GarfieldKwong 指点下发现这个版本动画播放完是可以触发消息机制的,更高级版本3.x支持的效果应该是在动画中就加入callback的关键帧

新技能get√

具体代码可见下面学习案例的第一个例子里Explosion部分

=====================

cocosbuider的学习可以看两个例子Creating a Game with CocosBuilder;Introduction to CocosBuilder;然后就是多用多尝试。

6

整个学习过程要学会查资料、学会寻求帮助,我主要用的stackoverflow(话说刚刚发现stackoverflow的reputation喜过15,终于可以投票了:)

在学习过程中得到了子龙山人(我认为本尊是这位 @屈光辉 )、LearnCocos2d作者Steffen、 @kubisoft 以及众多网上朋友的帮助,再次表示感谢。

7

硬件及开发者资格:看完c++开始看obj-c,这个过程一直是使用的vmware虚拟机安装的mac系统写程序;然后入手一个最便宜的mac pro、以及iPhone,尝试玩各种游戏;从看c++开始大约过了1年半(已经有非常简陋的游戏demo)才真正购入iOS开发者资格并开始真机测试。

8

尝试过的一些其他工具、各种弯路和坑、以及发散内容:

可以拖拽方式写代码的stencyl <Stencyl: Make iPhone, iPad, Android &Flash Games without code>;

最初学的不是ojc-c而是谭浩强老师的c++程序设计(虽然有人不喜谭老师,但那书是中文的让我对面对对象程序设计有一些初步的概念,再看英文的obj-c不至于太摸不着头脑,反正手头正好有这么一本买了6年没看的c++。。。);

尝试过<Tiled Map Editor>但自己目前的坑没有用到;

尝试过一点物理引擎和粒子,但自己目前的坑没有用到;现在粒子用到了,做了火焰等效果,也挺美的;但是一定要注意CPU占用优化,内存方面我最低支持4S没遇到大问题

学习音乐制作,这个我小时候学琴一直也喜欢音乐所以有一点点底子,如果没有基础可能上手有门槛;开始用的软件LMMS,但是音源插件在64位系统的问题我一直没解决掉时间精力也不多,所以是暂时停滞了;最近发现Garage Band可能也可以写歌;另外还试过一个很有历史的微软的音乐自动编辑器,很好玩,用来做背景音乐也不错。

入手手绘板学画画(这中间纠结了好久才决定用位图不用矢量图),如果你能找到热爱游戏的美术同学一起搞那最好不过。但是我一直对画画心痒难搔。。。所以入了这个深坑,晒一晒:

转载

z

Today, we’re going to build an interactive tic-tac-toe game.

If you like, you can check out the final result here: Final Result . Don’t worry if the code doesn’t make sense to you yet, or if it uses an unfamiliar syntax. We will be learning how to build this game step by step throughout this tutorial.

Try playing the game. You can also click on a button in the move list to go “back in time” and see what the board looked like just after that move was made.

Once you get a little familiar with the game, feel free to close that tab, as we’ll start from a simpler template in the next sections.

We’ll assume some familiarity with HTML and JavaScript, but you should be able to follow along even if you haven’t used them before.

If you need a refresher on JavaScript, we recommend reading this guide . Note that we’re also using some features from ES6, a recent version of JavaScript. In this tutorial, we’re using arrow functions , classes , let , and const statements. You can use the Babel REPL to check what ES6 code compiles to.

There are two ways to complete this tutorial: you could either write the code right in the browser, or you could set up a local development environment on your machine. You can choose either option depending on what you feel comfortable with.

This is the quickest way to get started!

First, open this starter code in a new tab. It should display an empty tic-tac-toe field. We will be editing that code during this tutorial.

You can now skip the next section about setting up a local development environment and head straight to the overview .

Alternatively, you can set up a project on your computer.

Note: this is completely optional and not required for this tutorial!

This is more work, but lets you work from the comfort of your editor.

If you want to do it, here are the steps to follow:

Now if you run npm start in the project folder and open http://localhost:3000 in the browser, you should see an empty tic-tac-toe field.

We recommend following these instructions to configure syntax highlighting for your editor.

If you get stuck, check out the community support resources . In particular, Reactiflux chat is a great way to get quick help. If you don’t get a good answer anywhere, please file an issue, and we’ll help you out.

With this out of the way, let’s get started!

React is a declarative, efficient, and flexible JavaScript library for building user interfaces.

React has a few different kinds of components, but we’ll start with React.Component subclasses:

We’ll get to the funny XML-like tags in a second. Your components tell React what you want to render – then React will efficiently update and render just the right components when your data changes.

Here, ShoppingList is a React component class , or React component type . A component takes in parameters, called props , and returns a hierarchy of views to display via the render method.

The render method returns a description of what you want to render, and then React takes that description and renders it to the screen. In particular, render returns a React element , which is a lightweight description of what to render. Most React developers use a special syntax called JSX which makes it easier to write these structures. The <div /> syntax is transformed at build time to React.createElement('div') . The example above is equivalent to:

See full expanded version.

If you’re curious, createElement() is described in more detail in the API reference , but we won’t be using it directly in this tutorial. Instead, we will keep using JSX.

You can put any JavaScript expression within braces inside JSX. Each React element is a real JavaScript object that you can store in a variable or pass around your program.

The ShoppingList component only renders built-in DOM components, but you can compose custom React components just as easily, by writing <ShoppingList />. Each component is encapsulated so it can operate independently, which allows you to build complex UIs out of simple components.

Start with this example: Starter Code .

It contains the shell of what we’re building today. We’ve provided the styles so you only need to worry about the JavaScript.

In particular, we have three components:

The Square component renders a single <button>, the Board renders 9 squares, and the Game component renders a board with some placeholders that we’ll fill in later. None of the components are interactive at this point.

Just to get our feet wet, let’s try passing some data from the Board component to the Square component.

In Board’s renderSquare method, change the code to pass a value prop to the Square:

Then change Square’s render method to show that value by replacing {/* TODO */} with {this.props.value} :

Before:

[站外图片上传中...(image-d45328-1515587905953)]

After: You should see a number in each square in the rendered output.

[站外图片上传中...(image-c8a682-1515587905953)]

View the current code.

Let’s make the Square component fill in an “X” when you click it. Try changing the button tag returned in the render() function of the Square like this:

If you click on a square now, you should get an alert in your browser.

This uses the new JavaScript arrow function syntax. Note that we’re passing a function as the onClick prop. Doing onClick={alert('click')} would alert immediately instead of when the button is clicked.

React components can have state by setting this.state in the constructor, which should be considered private to the component. Let’s store the current value of the square in state, and change it when the square is clicked.

First, add a constructor to the class to initialize the state:

In JavaScript classes , you need to explicitly call super() when defining the constructor of a subclass.

Now change the Square render method to display the value from the current state, and to toggle it on click:

Now the <button> tag looks like this:

Whenever this.setState is called, an update to the component is scheduled, causing React to merge in the passed state update and rerender the component along with its descendants. When the component rerenders, this.state.value will be 'X' so you’ll see an X in the grid.

If you click on any square, an X should show up in it.

View the current code.

The React Devtools extension for Chrome and Firefox lets you inspect a React component tree in your browser devtools.

[站外图片上传中...(image-d9c2c3-1515587905953)]

It lets you inspect the props and state of any of the components in your tree.

After installing it, you can right-click any element on the page, click “Inspect” to open the developer tools, and the React tab will appear as the last tab to the right.

However, note there are a few extra steps to get it working with CodePen:

We now have the basic building blocks for a tic-tac-toe game. But right now, the state is encapsulated in each Square component. To make a fully-working game, we now need to check if one player has won the game, and alternate placing X and O in the squares. To check if someone has won, we’ll need to have the value of all 9 squares in one place, rather than split up across the Square components.

You might think that Board should just inquire what the current state of each Square is. Although it is technically possible to do this in React, it is discouraged because it tends to make code difficult to understand, more brittle, and harder to refactor.

Instead, the best solution here is to store this state in the Board component instead of in each Square – and the Board component can tell each Square what to display, like how we made each square display its index earlier.

When you want to aggregate data from multiple children or to have two child components communicate with each other, move the state upwards so that it lives in the parent component. The parent can then pass the state back down to the children via props, so that the child components are always in sync with each other and with the parent.

Pulling state upwards like this is common when refactoring React components, so let’s take this opportunity to try it out. Add a constructor to the Board and set its initial state to contain an array with 9 nulls, corresponding to the 9 squares:

We’ll fill it in later so that a board looks something like

Board’s renderSquare method currently looks like this:

Modify it to pass a value prop to Square.

View the current code.

Now we need to change what happens when a square is clicked. The Board component now stores which squares are filled, which means we need some way for Square to update the state of Board. Since component state is considered private, we can’t update Board’s state directly from Square.

The usual pattern here is pass down a function from Board to Square that gets called when the square is clicked. Change renderSquare in Board again so that it reads:

We split the returned element into multiple lines for readability, and added parentheses around it so that JavaScript doesn’t insert a semicolon after return and break our code.

Now we’re passing down two props from Board to Square: value and onClick . The latter is a function that Square can call. Let’s make the following changes to Square:

After these changes, the whole Square component looks like this:

Now when the square is clicked, it calls the onClick function that was passed by Board. Let’s recap what happens here:

Note that DOM <button> element’s onClick attribute has a special meaning to React, but we could have named Square’s onClick prop or Board’s handleClick method differently. It is, however, conventional in React apps to use on* names for the attributes and handle* for the handler methods.

Try clicking a square – you should get an error because we haven’t defined handleClick yet. Add it to the Board class.

View the current code.

We call .slice() to copy the squares array instead of mutating the existing array. Jump ahead a section to learn why immutability is important.

Now you should be able to click in squares to fill them again, but the state is stored in the Board component instead of in each Square, which lets us continue building the game. Note how whenever Board’s state changes, the Square components rerender automatically.

Square no longer keeps its own stateit receives its value from its parent Board and informs its parent when it’s clicked. We call components like this controlled components .

In the previous code example, we suggest using the .slice() operator to copy the squares array prior to making changes and to prevent mutating the existing array. Let’s talk about what this means and why it is an important concept to learn.

There are generally two ways for changing data. The first method is to mutate the data by directly changing the values of a variable. The second method is to replace the data with a new copy of the object that also includes desired changes.

The end result is the same but by not mutating (or changing the underlying data) directly we now have an added benefit that can help us increase component and overall application performance.

Immutability also makes some complex features much easier to implement. For example, further in this tutorial we will implement time travel between different stages of the game. Avoiding data mutations lets us keep a reference to older versions of the data, and switch between them if we need to.

Determining if a mutated object has changed is complex because changes are made directly to the object. This then requires comparing the current object to a previous copy, traversing the entire object tree, and comparing each variable and value. This process can become increasingly complex.

Determining how an immutable object has changed is considerably easier. If the object being referenced is different from before, then the object has changed. That’s it.

The biggest benefit of immutability in React comes when you build simple pure components . Since immutable data can more easily determine if changes have been made, it also helps to determine when a component requires being re-rendered.

To learn more about shouldComponentUpdate() and how you can build pure components take a look at Optimizing Performance .

We’ve removed the constructor, and in fact, React supports a simpler syntax called functional components for component types like Square that only consist of a render method. Rather than define a class extending React.Component , simply write a function that takes props and returns what should be rendered.

Replace the whole Square class with this function:

You’ll need to change this.props to props both times it appears. Many components in your apps will be able to be written as functional components: these components tend to be easier to write and React will optimize them more in the future.

While we’re cleaning up the code, we also changed onClick={() =>props.onClick()} to just onClick={props.onClick} , as passing the function down is enough for our example. Note that onClick={props.onClick()} would not work because it would call props.onClick immediately instead of passing it down.

View the current code.

An obvious defect in our game is that only X can play. Let’s fix that.

Let’s default the first move to be by ‘X’. Modify our starting state in our Board constructor:

Each time we move we shall toggle xIsNext by flipping the boolean value and saving the state. Now update Board’s handleClick function to flip the value of xIsNext :

Now X and O take turns. Next, change the “status” text in Board’s render so that it also displays who is next:

After these changes you should have this Board component:

View the current code.

Let’s show when a game is won. Add this helper function to the end of the file:

You can call it in Board’s render function to check if anyone has won the game and make the status text show “Winner: [X/O]” when someone wins.

Replace the status declaration in Board’s render with this code:

You can now change handleClick in Board to return early and ignore the click if someone has already won the game or if a square is already filled:

Congratulations! You now have a working tic-tac-toe game. And now you know the basics of React. So you’re probably the real winner here.

View the current code.

你可以先去【绘学霸】网站找“游戏特效/unity3D”板块的【免费】视频教程-【点击进入】完整入门到精通视频教程列表: www.huixueba.net/web/AppWebClient/AllCourseAndResourcePage?type=1&tagid=305,306&zdhhr-11y17r-1369989254957618779

想要系统的学习可以考虑报一个网络直播课,推荐CGWANG的网络课。老师讲得细,上完还可以回看,还有同类型录播课可以免费学(赠送终身VIP)。

自制能力相对较弱的话,建议还是去好点的培训机构,实力和规模在国内排名前几的大机构,推荐行业龙头:王氏教育。

王氏教育全国直营校区面授课程试听【复制后面链接在浏览器也可打开】:

www.cgwang.com/course/gecoursemobilecheck/?zdhhr-11y17r-1369989254957618779

在“游戏特效/unity3D”领域的培训机构里,【王氏教育】是国内的老大,且没有加盟分校,都是总部直营的连锁校区。跟很多其它同类型大机构不一样的是:王氏教育每个校区都是实体面授,老师是手把手教,而且有专门的班主任从早盯到晚,爆肝式的学习模式,提升会很快,特别适合基础差的学生。

大家可以先把【绘学霸】APP下载到自己手机,方便碎片时间学习——绘学霸APP下载:www.huixueba.com.cn/Scripts/download.html