zi xautozi

《Cocos2d-x游戏开发实战精解》学习笔记2--在Cocos2d-x中显示一行文字 - andyou - 博客园
&在Cocos2d-x中要显示文字就需要用到Label控件。在3.x版本的Cocos2d中,舍弃了之前版本所使用的LabelTTF、LabelAtlas、LabelBMFont 3个用于显示文字的类,而使用一个新类Label取代。可想而知,Label实际上就是对之前3个类进行重新封装并统一渲染。
提示:实际上老版本的内容在3.x版本中依然是可以使用的。
下面是在Cocos2d-x中显示文字的一个例子,具体完整项目可在源文件本章目录下找到项目ChapterThree01查看。
【范例3-1&在Cocos2d-x中显示文字】
auto label1 = Label::create("this is the first label","Arial",36,
Size::ZERO,TextHAlignment::LEFT,TextVAlignment::TOP);
//创建标签
label1-&setPosition(210, 310);
//设置标签位置
addChild(label1);
//加入到场景中
//通过TTFConfig在标签中使用自定义字体
TTFConfig ttfconfig("fonts/chunkmuffinhollowwide.ttf", 36);
//创建TTFConfig对象
auto label2 = Label::createWithTTF(ttfconfig,"this is the second label");
//创建标签
label2-&setPosition(270,250);
//设置标签位置
addChild(label2);
//将标签加入到场景中
//使用createWithTTF方法创建使用自定义字体的标签
auto label3 = Label::createWithTTF("this is the third label","fonts/Marker Felt.ttf",36);
label3-&setPosition(200, 190);
addChild(label3);
//将标签加入到场景中
//为标签加入阴影和边框效果
auto label4 = Label::createWithTTF("this is the fourth label", "fonts/Marker Felt.ttf", 36);
label4-&setPosition(280,130);
//设置标签位置
label4-&enableShadow(Color4B::GREEN, Size(5, 5));
//设置阴影效果
label4-&enableOutline(Color4B::RED, 3);
//设置边框效果
addChild(label4);
//将标签加入到场景中
//创建带发光效果的标签
auto label5 = Label::createWithTTF("this is the fifth label", "fonts/Marker Felt.ttf", 36);
label5-&setPosition(200, 70);
//设置标签位置
label5-&enableGlow(Color4B::GREEN);
//设置光效
addChild(label5);
//将标签加入到场景中
运行之后的效果如图3-1所示。
图3-1&在屏幕上显示不同效果的标签
在一般情况下想要在屏幕上输出一行文字只需要按照第01~04行的方法来创建一个标签,然后再将它加入到场景中去就可以了。最简单的创建标签的方法如范例第01~02行所示,其中create方法的6个参数含义分别是:
l&&标签中所显示文字的内容
l&&标签字体
l&&标签内容字体大小
l&&标签尺寸
l&&标签中文字在横向和纵向的对齐方式
这看上去有点复杂,实在配不上&最简单&的创建方法,所以一般在使用时可以进一步简化,仅保留前3项参数,比如本范例第01行就可以改写成:
auto label1 = Label::create("this is the first label","Arial",36);
显示的文字如图3-1第一行所示,所以说在Cocos2d中显示文字是非常容易的。然而不能忘记的是,学习Cocos2d的目的主要还是为了做游戏,这样单调的文字自然是远远不能适应游戏的需要,所以就需要在标签中使用更加个性化的字体。
在Cocos2d 2.x版本中,可以使用LabelTTF类创建标签来使用TTF格式的字体,然而在Cocos2d 3.x中由于LabelTTF被取消(实际仍然可以使用),就要考虑使用其他办法来显示自定义字体。目前主要有两种方法:
l&&一种是通过TTFConfig配置来创建,如范例第06~09行所示;
l&&另一种是使用createWithTTF方法创建使用TTF字体的标签,如范例第11~13行所示。
这两种方法从本质上来说都是从资源目录下读取TTF格式的字体文件,可以在项目目录下找到Resources|fonts文件夹,将从网上下载的字体文件存放在这里。
为了让字体具有更强大的视觉冲击力,还可以给文字加入阴影和边框以及外发光的效果。如范例第17行就是给标签加入阴影,其中Color4B::GREEN是阴影的颜色而Size(5, 5)是阴影相对于文字的相对位置。第18行则是用来设置文字边框的,两个参数分别用来设置边框颜色和宽度。第23行为文字加入的外发光效果只有一个参数,那就是光效的颜色。Lamborghini recently unveiled their newest project, the Centenario. This car first enticed us at the Geneva Motor Show in March with the presentation of the 770-torque Centenario megacar, revealing a shut rooftop car yet encouraging us that an outstanding open-top Centenario Roadster would be coming soon. Most recently, the Lamborghini Centenario has gotten much more buzz since it appeared at Monterey Car Week.
| Posted in
If you enjoyed this post, then subscribe to the .
This year at the Monterey Car Week show, the US spotted its first road legal Koenigsegg Agera RS taken in the form of the very orange Agera XS. Even though that model may have amazed the many that have spotted the beast in person, they are still missing out on one that looks even better and that is Europe’s first Agera RS. Europe’s Agera RS, given the name “Naraya” will be unveiled in the coming weeks at the Salon Privé Concours d’Elégance in England.
Realistically the Naraya is almost as similar as the Agera XS, but certain details of the vehicle has been customized to meet the owner’s request. The Agera RS has a body that has been finished with a blue tinted carbon fiber which was a very first for Koenigsegg. In addition, to the blue carbon fiber finish, the Naraya also possess an excessive amount of gold leaf on the interior and exterior of the vehicle.
| Posted in
If you enjoyed this post, then subscribe to the .
Earlier this week it was reported that Dwayne “The Rock” Johnson was voicing his frustration with some of his male co-stars. In a recent Instagram post, Johnson praised his female co-stars and how it was amazing working with them during the eighth installment of The Fast and the Furious series, but further down in the post he took shots at one of his male co-stars who we now know was Vin Diesel.
A report from TMZ has claimed that the star and Vin Diesel were butting heads during the production of the film.
| Posted in
If you enjoyed this post, then subscribe to the .
Although finding a Ferrari F40 to purchase isn’t that difficult, finding the right one, in good condition and etc, could be. However, one very fine F40 is currently up for auction on Craigslist.
Originally only 1,300 F40s were manufactured. And since then, many supercar connoisseurs consider the F40 to be one of the best exotic cars ever produced.
Currently, craigslist has an F40 up for sale for a price tag of $1.6 million. The F40 is the 1990 model and, regardless of being 26 years of age, just has 2,000 miles on the odometer. The car has even been certified by the Ferrari Classiche Certification. Likewise with all F40s, it is painted in the Italian organization’s unmistakable shade of Rosso Corsa.
| Posted in
If you enjoyed this post, then subscribe to the .
The new 2017 Bugatti Chiron worth $2.5 million dollars isn’t even on sale yet or even production ready, yet someone has already outbid many other competitors to become the sole owner of the two supercars. Many of the wealthiest gathered around and reached deep into their pockets in an attempt to outbid one another to own one of these magnificent beasts. You can just ask Prince Badr Bin Saud of Saudi Arabia who was the highest bidder for the supercars.
| Posted in
If you enjoyed this post, then subscribe to the .
As the years have passed many automobile companies are jumping on the bandwagon of joining the electric vehicle market as it is becoming the new eco-friendly trend in this decade. We have seen Mercedes, BMW, Nissan and much more begin to join the electric vehicle market and put a lot of their effort into creating one of the best EV’s out there.
Porsche has recently made an effort in to trying to get their own concept of an electric vehicle on the road, by hiring an additional 400 people to the already 1,000 other workers who were previously hired.
| Posted in
If you enjoyed this post, then subscribe to the .
Jaguar is abandoning all plans to add to the supercar/sports car section of their future lineup. Any plans to create the next XK coupe/convertible as well as the production variant of the C-X75 concept supercar have been tossed out the window. Jaguar made it clear that the XK could be converted into a Grand Tourer, however, no further information on the topic has been released on the topic as of now.
Instead, Jaguar has focuses its attention on building two new luxury EV’s. One will be an SUV and the other, a replacement for the XJ sedan.
The large EV, which is said to be named X590, is reported to be a four-door coupe with a rear hatch. The X590 is said to have a new electronic architecture which can accommodate the software that is capable of autonomous driving.
Jaguar wishes to take on Tesla and the other electronic vehicles coming out of Germany with the X590.
Jaguar expects to put 20,000 to 30,000 X590’s on the road each year. The SUV, which is predicted to be more affordable. Jaguar surely is looking to capitalize on these segments for the future of their lineup.
| Posted in
If you enjoyed this post, then subscribe to the .
Last week, the driver of an ultra rare Koenigsegg One:1 crashed his car at the Nurburgring.
There were only seven made in the whole world, and the current value of one is upwards of $2 million USD. However, a dealership in Australia had acquired theirs for $7.4 Million.
The Nurburgring has always been known as “The Green Hell” and we’re sure that this driver would remember that name as he walked away from this very, very expensive crash.
| Posted in
If you enjoyed this post, then subscribe to the .
The new Lamborghini Huracan is a lighter, faster and more powerful version of the current after undertaking some of the latest advancement testing.
For the first time, we got to recently see the prototype take to the track. Reports say that the new Huracan will make utilization of the Superleggera nameplate as it will be lighter by 100kg/220lbs with the focused on utilization of lightweight materials in the suspension and bodywork.
| Posted in
If you enjoyed this post, then subscribe to the .
Hi, I'm . I'm a full time car aficionado, part time blogger, and a certified gentlemen.新浪汽车 - 汽车生活源动力!
共9个车型,含35043张图片
找车&询价&购车计算
选择车型(选填)
记住该车辆信息
新车未上牌
按价格找车
扫二维码进入
查看更多车型
购车完全指南110期
说明:s5=5颗星,s4=4颗星,依此类推
14.98-16.98万元
18.98-31.69万元
39.6-57.9万元
7.78-9.48万元
9.98-15.38万元
19.98-31.58万元
21.99-34.99万元
9.28-16.38万元
17.98-26.98万元
38.34-56.7万元
|||||||||||||||||||||
||||||||||||||||||
|||||||||||||||||
||||||||||
|||||||||||||||||||||
||||||||||||||||||
|||||||||||||||||
||||||||||
*手机号码:
*所在城市:
接收最新降价提醒
*底价将以短信的形式发送到您的手机,个人信息不会泄露给第三方
*手机号码:
*订阅信息将以短信的形式发送到您的手机,个人信息不会泄露给第三方新浪汽车 - 汽车生活源动力!
共9个车型,含35043张图片
找车&询价&购车计算
选择车型(选填)
记住该车辆信息
新车未上牌
按价格找车
扫二维码进入
查看更多车型
购车完全指南110期
说明:s5=5颗星,s4=4颗星,依此类推
14.98-16.98万元
18.98-31.69万元
39.6-57.9万元
7.78-9.48万元
9.98-15.38万元
19.98-31.58万元
21.99-34.99万元
9.28-16.38万元
17.98-26.98万元
38.34-56.7万元
|||||||||||||||||||||
||||||||||||||||||
|||||||||||||||||
||||||||||
|||||||||||||||||||||
||||||||||||||||||
|||||||||||||||||
||||||||||
*手机号码:
*所在城市:
接收最新降价提醒
*底价将以短信的形式发送到您的手机,个人信息不会泄露给第三方
*手机号码:
*订阅信息将以短信的形式发送到您的手机,个人信息不会泄露给第三方}

我要回帖

更多关于 autozi.com 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信