Lifecycle
์๊ฐ
์ฑ์ด ์์๋์ด, ์คํ๋๊ณ ์ข ๋ฃ๋ ๋๊น์ง๋ฅผ ํ๋์ ์์ ์ฃผ๊ธฐ(Lifecycle)๋ผ๊ณ ํฉ๋๋ค. ๊ด๋ จ ํจ์๋ค์ ์ด์ฉํ์ฌ ์ฑ์ด ์์๋ ๋, ์คํ ์ค์ผ ๋ ๊ทธ๋ฆฌ๊ณ ์ข ๋ฃ๋ ๋ ๋ฑ์ ์ํฉ์์ ํ์ํ ๋์๋ค์ ์คํํด ์ ์ฒด์ ์ธ ์ฑ์ ์์ ์ฃผ๊ธฐ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
onInit
App์ด ์ต์ด๋ก ์์๋ ๋ ํ ๋ฒ ํธ์ถ๋๋ ํจ์์ ๋๋ค.
onJoinPlayer
onInit์ด ํธ์ถ๋ ํ, ์ ์ํด ์๋ ๋ชจ๋ ํ๋ ์ด์ด๋ฅผ ํด๋น ์ด๋ฒคํธ๋ฅผ ํตํด ์ ์ฅ ์ํค๊ณ , ์ดํ ์ ์ฅํ๋ ํ๋ ์ด์ด๊ฐ ์์ ๋ ๋ง๋ค ๋์ํฉ๋๋ค.
onStart
๋ชจ๋ ํ๋ ์ด์ด๊ฐ onJoinPlayer๋ฅผ ํตํด ์ ์ฅํ ํ ํ ๋ฒ ํธ์ถ ๋ฉ๋๋ค.
onUpdate
์ฝ 20ms ๋ง๋ค ์ฃผ๊ธฐ์ ์ผ๋ก ์คํ๋๋ ํจ์์ ๋๋ค.
onLeavePlayer
ํด์ฅํ๋ ํ๋ ์ด์ด๊ฐ ์์ ๋ ๋ง๋ค ๋์ํฉ๋๋ค. ์ดํ, ๋ค๋ฅธ App์ด ์คํ๋๊ฑฐ๋ ์ค์นํ Game Block์ด ํ๊ดด๋ ๋ ๋ชจ๋ ํ๋ ์ด์ด๋ฅผ ์ด ํจ์๋ฅผ ํตํด ํด์ฅ์ํต๋๋ค.
onDestroy
๋ค๋ฅธ App์ด ์คํ๋๊ฑฐ๋ ์ค์นํ Game Block์ด ํ๊ดด๋ ๋ ๋์ํฉ๋๋ค.
App ์์ ์ฃผ๊ธฐ(Lifecycle) ์ดํดํ๊ธฐ
Lifecycle ํจ์๋ค์ ์์ ์ฃผ๊ธฐ์ ๋ง๊ฒ ๊ธฐ๋ฅ์ ๋ง๋ค ์ ์๋ ํ์์ ์ธ ํจ์์ ๋๋ค. ์๋ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ์ฑ์ด ์คํ๋ ๋๋ Enter ๋จ๊ณ์ ํจ์๋ค์ด ๋์ํ๊ณ , ์ฑ์ด ์คํ ์ค ์ผ๋๋ Update ๋จ๊ณ์ ํจ์๋ค์ด ์ฃผ๊ธฐ์ ์ผ๋ก ๋์ํ๋ฉฐ, ์ฑ์ด ์ข ๋ฃ๋ ๋๋ Exit ๋จ๊ณ์ ํจ์๋ค์ด ๋์ํ๊ฒ๋ฉ๋๋ค.
์ฑ์ด ์คํ๋์ด ์ข ๋ฃ๋ ๋ ๊น์ง ๊ฐ ๋จ๊ณ์ ํจ์๋ค์ ์ ํ์ฉํด ์์ ์ฃผ๊ธฐ์ ๋ง๊ฒ App์ ๋ง๋ค์ด๋ณด์ธ์.

Lifecycle ํจ์ ํ ๋์ ๋ณด๊ธฐ
// App ์คํ ์์ ์ต์ด๋ก ํธ์ถ๋๋ ์ด๋ฒคํธ (์ ์ ์ง์
์ )
// Normal App๊ณผ Sidebar App์ Script ์ ์ฉ ํ ๋งต์ด ์คํ๋ ๋ ํธ์ถ [ Enter ]
App.onInit.Add(function(){
});
// ๋ชจ๋ ํ๋ ์ด์ด๋ฅผ ์ด ์ด๋ฒคํธ๋ฅผ ํตํด App์ ์ง์
์ํด [ Enter ]
// ์ดํ ํ๋ ์ด์ด๊ฐ ์
์ฅ ํ ๋๋ง๋ค ํธ์ถ [ Events ]
App.onJoinPlayer.Add(function(player) {
});
// ํ๋ ์ด์ด ๋ชจ๋ ์ง์
์ ์ต์ด๋ก ์์๋๋ ์ด๋ฒคํธ [ Enter ]
App.onStart.Add(function(){
});
// 20ms ๋ง๋ค ํธ์ถ๋๋ ์ด๋ฒคํธ
// dt: deltatime(์ ํ๋ ์์ด ์๋ฃ๋๊ธฐ๊น์ง ๊ฑธ๋ฆฐ ์๊ฐ) [ Update ]
App.onUpdate.Add(function(dt){
});
// ์ด๋ฒคํธ ์ฝ๋ฐฑ ์ฒ๋ฆฌ ํ ๋ค์ onUpdate
// App ์ข
๋ฃ ์ ๋ชจ๋ ํ๋ ์ด์ด๋ฅผ App์์ ๋๊ฐ๊ฒ ํจ [ Exit ]
App.onLeavePlayer.Add(function(player){
});
// App ์ข
๋ฃ ์ ๋ง์ง๋ง์ผ๋ก ํธ์ถ [ Exit ]
// Normal App๊ณผ Sidebar App์ ๋ณ๋์ ์ข
๋ฃ
App.onDestroy.Add(function(){
});
1๏ธโฃ Enter ๋จ๊ณ ํจ์
App์ ์คํ๊ณผ ํจ๊ป ํธ์ถ๋๋ ๋ผ์ดํ์ฌ์ดํด Enter ๋จ๊ณ์์ ํธ์ถ๋๋ ํจ์๋ฅผ ์๋ดํฉ๋๋ค.
onInit
App์ด ์ต์ด๋ก ์์๋ ๋ ํ ๋ฒ ํธ์ถ๋ฉ๋๋ค.
ํ๋ผ๋ฏธํฐ
์์
์์
onInit ์์ ์ฑํ ์ถ๋ ฅํด๋ณด๊ธฐ. ( ๋ฏธ๋๊ฒ์์ผ๋ก ๋ง๋ค์ด ํ์ธํด๋ณด์ธ์. )
App.onInit.Add(function(){
App.sayToAll("-- onInit --")
App.sayToAll(" ready.. ")
App.sayToAll("------------")
});
onJoinPlayer
onInit์ด ํธ์ถ๋ ํ, ์ ์ํด ์๋ ๋ชจ๋ ํ๋ ์ด์ด๋ฅผ ํด๋น ์ด๋ฒคํธ๋ฅผ ํตํด ์ ์ฅ์ํค๊ณ , ์ดํ ์ ์ฅํ๋ ํ๋ ์ด์ด๊ฐ ์์ ๋ ๋ง๋ค ๋์ํฉ๋๋ค.
ํ๋ผ๋ฏธํฐ
player
Player
player๋ ์ ์ฅํ๋ ํ๋ ์ด์ด๋ฅผ ๊ฐ๋ฅดํด ํ๋ผ๋ฏธํฐ์ ์ด๋ฆ์ ์์๋ก ๋ณ๊ฒฝ๊ฐ๋ฅ
์์
ํ๋ ์ด์ด ์ ์ฅ์ ๋ฉ์์ง ์ถ๋ ฅํด๋ณด๊ธฐ
App.onJoinPlayer.Add(function(player){
App.showCenterLabel(`${player.name}๋์ด ์
์ฅํ์
จ์ต๋๋ค.`)
});
onStart
๋ชจ๋ ํ๋ ์ด์ด๊ฐ onJoinPlayer๋ฅผ ํตํด ์ ์ฅํ ํ ํ ๋ฒ ํธ์ถ ๋ฉ๋๋ค.
ํ๋ผ๋ฏธํฐ
์์
์์
onStart์์ ์ฑํ ์ถ๋ ฅํด๋ณด๊ธฐ ( ๋ฏธ๋๊ฒ์์ผ๋ก ๋ง๋ค์ด ํ์ธํด๋ณด์ธ์. )
App.onStart.Add(function(){
App.sayToAll("-- App Start --")
});
Enter ๋จ๊ณ ํจ์์ ํ๋ฆ ์ดํดํ๊ธฐ
Lifecycle Enter ๋จ๊ณ์ ํ๋ฆ์ ์ฝ๋๋ก ํ์ธํด๋ณด์ธ์. ์๋ ์ฝ๋๋ฅผ ๋ฏธ๋๊ฒ์์ผ๋ก ๋ง๋ค์ด ์คํํด๋ณด์ธ์!
// main.js
App.onInit.Add(function(){
App.sayToAll("-- onInit --")
App.sayToAll(" ready.. ")
App.sayToAll("------------")
});
App.onJoinPlayer.Add(function(player){
App.sayToAll(`${player.name}๋์ด ์
์ฅํ์
จ์ต๋๋ค.`)
});
App.onStart.Add(function(){
App.sayToAll("-- App Start --")
});

2๏ธโฃ Update
Update์๋ ์ฝ 20ms ๋ง๋ค ์ฃผ๊ธฐ์ ์ผ๋ก ์คํ๋๋ ํจ์ onUpdate๊ฐ ์์ต๋๋ค.
onJoinPlayer, onLeavePlayer ๋ฑ์ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ฉด ํด๋น ์ด๋ฒคํธ ์ฒ๋ฆฌ ํ ๋ค์ onUpdate๊ฐ ์ฃผ๊ธฐ์ ์ผ๋ก ์คํ๋ฉ๋๋ค.
onUpdate
์ฝ 20ms ๋ง๋ค ์ฃผ๊ธฐ์ ์ผ๋ก ์คํ๋๋ ํจ์์ ๋๋ค.
ํ๋ผ๋ฏธํฐ
dt
Number
deltatime (์ ํ๋ ์์ด ์๋ฃ๋๊ธฐ๊น์ง ๊ฑธ๋ฆฐ ์๊ฐ, ์ฝ 20ms) dt ํ๋ผ๋ฏธํฐ์ ์ด๋ฆ์ ์์๋ก ๋ณ๊ฒฝ ๊ฐ๋ฅ
์์
onUpdate ํจ์๋ฅผ ์ด์ฉํด 10์ด ์นด์ดํธ๋ค์ด ๋ง๋ค์ด๋ณด๊ธฐ

let countdown = 10;
let timer = 0;
App.onUpdate.Add(function(dt){
timer += dt;
if(timer >=1){
if(countdown >= 0)
{
App.showCenterLabel(countdown--);
}
else{
App.showCenterLabel("Time Over");
}
timer = 0;
}
})
3๏ธโฃ Exit ๋จ๊ณ ํจ์
์ฑ์ด ์ข ๋ฃ๋ ๋ ์คํ๋๋ ํจ์ ์ ๋๋ค.
onLeavePlayer
ํด์ฅํ๋ ํ๋ ์ด์ด๊ฐ ์์ ๋ ๋ง๋ค ๋์ํฉ๋๋ค. ์ดํ, ๋ค๋ฅธ App์ด ์คํ๋๊ฑฐ๋ ์ค์นํ Game Block์ด ํ๊ดด๋ ๋ ๋ชจ๋ ํ๋ ์ด์ด๋ฅผ ์ด ํจ์๋ฅผ ํตํด ํด์ฅ์ํต๋๋ค.
ํ๋ผ๋ฏธํฐ
player
Player
player ํ๋ผ๋ฏธํฐ๋ ํด์ฅํ๋ ํ๋ ์ด์ด๋ฅผ ๊ฐ๋ฅดํด player ํ๋ผ๋ฏธํฐ์ ์ด๋ฆ์ ์์๋ก ๋ณ๊ฒฝ ๊ฐ๋ฅ
์์
ํ๋ ์ด์ด ํด์ฅ ์ ๋ฉ์์ง ์ถ๋ ฅํด๋ณด๊ธฐ
// ํ๋ ์ด์ด๊ฐ ํด์ฅํ ๋ ์คํ
App.onLeavePlayer.Add(function(player){
App.showCenterLabel(`${player.name}๋์ด ํด์ฅํ์
จ์ต๋๋ค.`)
});
onDestroy
๋ค๋ฅธ App์ด ์คํ๋๊ฑฐ๋, ์ค์นํ Game Block์ด ํ๊ดด๋ ๋ ๋์ํฉ๋๋ค.
ํ๋ผ๋ฏธํฐ
์์
์์
Game Block ํ๊ดด ์ ๋ฉ์์ง ์ถ๋ ฅํด๋ณด๊ธฐ ( ๋ฏธ๋๊ฒ์ )

// Game Block ํ๊ดด ์ ์คํ
App.onDestroy.Add(function(){
App.showCenterLabel("๊ฒ์ ๋ธ๋ก์ด ํ๊ดด๋์์ต๋๋ค.")
});
Last updated
Was this helpful?