.
106 Construct2 Create Card Games
OBJECTIVE:
This tutorial summarize the previous 101-105 tutorials
1) Create New Project
Select New empty SD portrait 4:3 project
Rename the project as CardGame106.
Optionally, rename the Layout and Sheet as LayoutHome and EventHome.
Create Play subfolder in Objects.
2) Create Shuffle Group
We create this group to randomize our game objects events and actions.
2.1) Insert Dictionary Object to the PlayObjects.
Name it as DictionaryShuffle.
2.2) Add Event.
Go to EventHome tab and click Add Event.
Add Event, System/On Start Of Layout.
2.3) Add Group
GroupShuffleIndexes
2.4) Add local variables to support shuffling
-Local number randomUpperBound=0
-Local text swapValue=””
-Local text swapKey1=””
-Local text swapKey2=””
2.5) Initialize Loop Variable
Add Blank Sub Event.
Add Action, System/Set varRandomUpperBound=5
2.6) Initialize DictionaryShuffle
Add new event below the current blank event.
Insert Event, System/Repeat = varRandomUpperBound.
Add Action, DictionaryShuffle/AddKey/
Key= str(loopindex)
Value=loopindex
(We are initializing the Dictionary Shuffle with a sequential number series starting from 0 to the varRandomIndexTotal)
Run Debug Mode to check that initialization works.
2.7) Shuffle Indexes
Add new event below the current event.
Add Event System/Repeat At varRandomUpperBound.
Add Action, System/SetValue/
swapkey1=str(floor(random(0,randomUpperBound)))
Add action, System/SetValue/
swapkey2=str(floor(random(0,randomUpperBound)))
Add another event containing three related conditions.
Three related sub-conditions:
1.Check that DictionaryShuffle has a key = swapkey1
2.Check that DictionaryShuffle has a key = swapkey2
3.Check that swapkey1≠swapkey2
Add action to perform swap if the three sub events above are true.
Add Action/System/SetValue
variable=swapValue
value=DictionaryShuffle.Get(swapKey1)
Add Action/DictionaryShuffle/SetKey
key=swapkey1
value=DictionaryShuffle.Get(swapKey2)
Add Action/DictionaryShuffle/SetKey
key=swapkey2
value=swapValue
Run Debug. Check that randomize works.
DOWNLOAD: CardGame106A.capx
3) Create Play Setup
3.1) Create the following Play Objects.
-SpritePlaySpot
-TextInstructions
-TextTarget
-TextPlay
3.2) Put PlayObjects on LayoutHome.
Name
|
SpritePlaySpot
|
Position
|
150,300
|
Size
|
1,1
|
Text
|
A
|
Horizontal Alignment
|
Center
|
Vertical Alignment
|
Center
|
HotSpot
|
Center
|
Name
|
TextInstructions
|
Position
|
10,10
|
Size
|
460,30
|
Text
|
Put the cards in right order
|
Horizontal Alignment
|
Center
|
Vertical Alignment
|
Center
|
HotSpot
|
Top Left
|
Name
|
TextPlay
|
Position
|
150,300
|
Size
|
30,30
|
Text
|
A
|
Horizontal Alignment
|
Center
|
Vertical Alignment
|
Center
|
HotSpot
|
Center
|
Instance Variable
|
SpotId=”A”
|
Name
|
TextTarget
|
Position
|
150,150
|
Size
|
30,30
|
Text
|
-
|
Horizontal Alignment
|
Center
|
Vertical Alignment
|
Center
|
HotSpot
|
Center
|
Instance Variable
|
SpotId=”A”
|
3.3) Add Behaviors to Objects.
TextPlay = DragDrop, Flash.
SpriteDragSpot = Pin
3.4) Create Drag Object Container
Container = TextPlay
Member = SpritePlaySpot
3.4) Set Actions For Play Objects
DOWNLOAD: CardGame106B.capx
4) Load Random Values To Play Objects
4.1) Arrange the Statement Groups
Check that statement groups are located at the right place.
GroupShuffleIndexes should be under System/On-start-of-layout event (shuffle is done only once at during the start of the layout)
GroupPlay should be outside of the System/On-start-of-layout event (as the behaviors will persist throughout the game run time)
4.1) Create Play Group Instances
Create them under GroupShuffleIndexes (as they will be created during the start of the layout)
Create SubGroups
Name=GroupDestroyInitialObjects
Name=GroupTextTargets
Name=GroupTextPlays
4.2) Destroy Initial Objects
Under the GroupDestroyInitialObjects, add a blank sub-event, add action to destroy TextTarget and TextPlay objects.
4.3) Create TextTarget Instances
For this tutorial, we set five instances (randomUpperBound=5).
We will assign numbers from 0 to 5 to each of the five instances.
Under GroupTextTargets…
add Event…
System/repeat-randomUpperBound-times
add Action…
System/Create Object TextTarget
x=TextTarget.X +loopindex*50
y=TextTarget.Y
add Action …
TextTarget/SetValue...
SpotId=str(loopindex)
Run Debug and check that SpotId has been set to the correct values.
OUTCOME.
4.4) Create TextPlay Instances
Under GroupTextPlays…
add Event…
System/repeat-randomUpperBound-times
add Action…
System/Create Object TextPlay
x=TextPlay.X +loopindex*50
y=TextPlay.Y
add Action …
TextPlay/SetValue...
SpotId=str(loopindex)
TextPlay/SetText
Text=DictionaryShuffle.Get(str(loopindex))
Run Debug.
OUTCOME.
PLAY.
DOWNLOAD: CardGame106C.capx
.
No comments:
Post a Comment