• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16@Component
17@Entry
18struct ParentComponent {
19    @State countDownStartValue: number = 10; // 10 Nuggets default start value in a Game
20    build() {
21        Column() {
22            Text(`Grant ${this.countDownStartValue} nuggets to play.`)
23            Button() {
24                Text("+1 - Nuggets in New Game")
25            }.onClick(() => {
26                this.countDownStartValue += 1
27            })
28            Button() {
29                Text("-1  - Nuggets in New Game")
30            }.onClick(() => {
31                this.countDownStartValue -= 1
32            })
33
34            // when creatng ChildComponent, the initial value of its @Prop variable must be supplied in a named constructor parameter
35            // also regular costOfOneAttempt (non-Prop) variable is initialied
36            CountDownComponent({ count: this.countDownStartValue, costOfOneAttempt: 2 })
37        }
38    }
39}
40
41@Component
42struct CountDownComponent {
43
44    @Prop count: number
45    costOfOneAttempt?: number
46
47    build() {
48        Column() {
49            if (this.count> 0) {
50                Text(`You have ${this.count} Nuggets left`)
51            } else {
52                Text("Game over!");
53            }
54
55            Button() {
56                Text("Try again")
57            }.onClick(() => {
58                this.count -= this.costOfOneAttempt;
59            })
60        }
61    }
62}
63