
Change orientation with device position
Decorator and State Patterns in Mobile Game Development
While I’m trying to find the best graphic tool to use in conjunction with mobile devices and ActionScript 3.0, I’ve decided to stick with wholly programmed images. (I’m a better programmer than artist, but my images do not improve because they’re programmed.) For this example, I used the
ActionScript Mobile Project option in
Flash Builder 4.5.1. If you’re using
Flash Pro 5.5, you can use the same code by selecting
AIR for iOS from the Flash main start-up menu. With both, everything is pure ActionScript 3.0—none of the drawing tools, components or other gizmos available in either FB or FP are employed—
código puro in both. Click the button below to download the files for Flash Builder. (I also included the files for Flash Pro; so no one is left out.)
Reminder: We’ve placed all of the downloads on the University of Hartford’s Multimedia Web Design and Development download page. Just select the bottom on the ActionScript 3.0 files and click the download button.
Flash Builder Tip: First create an ActionScript Project so that you can test it as you develop. Once you have it all developed, create an ActionScript Mobile Project and just copy and paste the files from the AS Project.
Re-Decorating
In this first part all I want to do with this mobile game is to build and decorate the warrior and put it on an iOS mobile device using Flash Builder. The mechanics are very similar to previous posts on using the Decorator pattern to arm a character (See here and here.) Figure 1 shows a file diagram of the Decorator implementation:

Figure 1:Decorator File Diagram
Since this thing is going to be in pure code, we need to create a “warrior” in code, which was not done in the previous posts linked above. So outside of the pattern, we need a graphic image, as provided in the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
| package
{
import flash.display.Shape;
import flash.display.Sprite;
public class BuildWarrior extends Sprite
{
private var head:Shape=new Shape();
private var torso:Shape=new Shape();
private var lftArm:Shape=new Shape();
private var rtArm:Shape=new Shape();
private var lftLeg:Shape=new Shape();
private var rtLeg:Shape=new Shape();
public function assemble():Sprite
{
this.addChild(makeHead());
this.addChild(makeTorso());
this.addChild(makeLftArm());
this.addChild(makeRtArm());
this.addChild(makeLftLeg());
this.addChild(makeRtLeg());
return this;
}
private function makeHead():Shape
{
head.graphics.lineStyle(1,0x6F74B3);
head.graphics.beginFill(0x6F91A6,.50);
head.graphics.drawRect(30,17,10,3);
head.graphics.lineStyle(0.25,0x0066ff);
head.graphics.beginFill(0x035AA6,.40);
head.graphics.drawCircle(35, 7,10);
head.graphics.lineStyle(1,0x6F74B3);
head.graphics.moveTo(30,6);
head.graphics.lineTo(34,6);
head.graphics.moveTo(36,6);
head.graphics.lineTo(40,6);
head.graphics.moveTo(32,12);
head.graphics.lineTo(38,12);
graphics.endFill();
return head;
}
private function makeTorso():Shape
{
torso.graphics.lineStyle(1,0x6F74B3);
torso.graphics.beginFill(0x6F91A6,.50);
torso.graphics.drawTriangles(Vector.<number>([10,20, 60,20, 35,90]));
torso.graphics.endFill();
return torso;
}
private function makeLftArm():Shape
{
lftArm.graphics.lineStyle(1,0x6F74B3);
lftArm.graphics.beginFill(0x6FB3A7,.75);
lftArm.graphics.drawRect(10,20,10,40);
lftArm.graphics.drawRect(10,60,40,8);
lftArm.graphics.endFill();
return lftArm;
}
private function makeRtArm():Shape
{
rtArm.graphics.lineStyle(1,0x6F74B3);
rtArm.graphics.beginFill(0x6FB3A7,.75);
rtArm.graphics.drawRect(50,20,10,40);
rtArm.graphics.drawRect(51,60,8,40);
rtArm.graphics.endFill();
return rtArm;
}
private function makeLftLeg():Shape
{
lftLeg.graphics.lineStyle(1,0x6F74B3);
lftLeg.graphics.beginFill(0x6FB3A7);
lftLeg.graphics.drawRect(23,70,12,45);
lftLeg.graphics.drawRect(24,115,10,45);
lftLeg.graphics.endFill();
return lftLeg;
}
private function makeRtLeg():Shape
{
rtLeg.graphics.lineStyle(1,0x6F74B3);
rtLeg.graphics.beginFill(0x6FB3A7);
rtLeg.graphics.drawRect(36,70,12,45);
rtLeg.graphics.drawRect(37,115,10,45);
rtLeg.graphics.endFill();
return rtLeg;
}
}
} |
All of the concrete decorators similarly implement coded graphic representations. Of course, all of these are “outside” of the design pattern but are useful to see in the contact of the pattern.
The Client
In the actual mobile client, you begin with a super(); call and support for auto orientation in the iOS device. The code is automatically generated:
1
2
3
| // support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE; |
I tossed out the super(); call, and generated the following client:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
public class MobileDecState extends Sprite
{
private var warrior:Component=new Warrior();
private var warriorSpirit:Sprite=new Sprite();
public function MobileDecState()
{
warriorSpirit.addChild(warrior.getarmor());
warriorSpirit.x=50,warriorSpirit.y=50;
addChild(warriorSpirit);
warrior=new DecBodyArmor(warrior);
warriorSpirit.addChild(warrior.getarmor());
warrior=new DecShield(warrior);
warriorSpirit.addChild(warrior.getarmor());
warrior=new DecHelmet(warrior);
warriorSpirit.addChild(warrior.getarmor());
warrior=new DecParticleCompressor(warrior);
warriorSpirit.addChild(warrior.getarmor());
// support autoOrients
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
}
}
} |
Publishing for Mobile
For some reason, more steps are involved in publishing for Flash Builder than Flash Pro. However, once you have your certificate (it has a .p12 extension) and your provisioning file created using the tools available to Apple developers; it’s pretty simple. If you don’t know about the Apple iOS developer program for both Windows and Mac users, you need to go there first.
Here’s how to publish using Flash Builder: (Download the Beta version/update of Flash Builder 4.5.1. While you’re at it, download the beta version of Adobe AIR 3.)
1. Open a New > New ActionScript Mobile Project (Figure 2)

Figure 2: Open a New ActionScript Mobile Project
2. Specify your target platform as Apple iOS with Automatically reorient selected and click Next. (Figure 3)

Figure3: Mobile Settings
3. Click the Platform Setting and select Platform and Target devices (Figure 4)

Figure 4: Specify Platformsettings
4. Build Paths provides the opportunity to add SWC folders or a project. Figure 5 shows using the default settings.

Figure 5: Include additional or different paths if requires
5. The next step in creating your app will not come until you are finished coding it all. Then when you press the green arrow (Run) button at the top, you will begin the Run Configurations sequence. Figure 6 shows the On device radio button selected. You are likely to see some red circles with white ‘X’s in them, but don’t worry. If there’s one next to the Packaging setting click the [Configure] link.

Figure 6: Run Configurations
6. Set the Certificate and Provisioning file. Both of these should be ready using the software that the Apple iOS Developer program provides. Use the Browse buttons to locate yours. (These cannot be generated using Adobe software.)

Figure 7: Setting Certificate and Provisioning files
Once the certificate and provisioning file are set, click the Apply button and then click OK.
7. If all has gone right you will see the Packaging Completed window shown in Figure 8. At this point you will have to open iTunes on your computer, and make sure that your mobile device is connected to your computer. You will need to locate the .ipa package and drop it into the iTunes library. It’s easiest to just click the (Reveal package in Finder) to get the .ipa package.

Figure 8: Completed Window
8. Figure 9 shows the bin-debug folder opened with the .ipa package. Be sure that the Apps window on your iTunes library is selected and drag the .ipa package into the iTunes library.

Figure 9 Drag .ipa package into iTunes Apps Library
9. The final step requires that you select the connected device (shown right above Little Richard Greatest Songs in Figure 10 ) and then find your new app in the Sync Apps window. Be sure to check the box next to the app. Then click the Apply button. Clicking the Apply button will then begin the Sync process. Once it is finished you should see the icon.

Figure 10: Adding the app to an iOS device
When you click the icon on your iOS mobile device, you will see the image shown at the top of this post. If you turn your device, the object will re-orient itself to the object’s orientation.
Adding the State Design
With this and the previous posts for mobile devices, you’ve seen that design patterns work well with mobile app development. All of the advantages found in design pattern development for computers can be found in creating applications for mobile devices.
In the next post, this process will be continued by adding a State pattern to the app to create a game. If you don’t have a mobile device, you can use the same coding for making the app for your computer.
Recent Comments