[Cocos 2D] Actions

SYieeยท2022๋…„ 10์›” 19์ผ
0

๊ฒŒ์ž„ํ”„๋กœ๊ทธ๋ž˜๋ฐ

๋ชฉ๋ก ๋ณด๊ธฐ
6/6
post-thumbnail

Settings

import cocos
import cocos.actions as ac

Interval Actions

๐Ÿ“Œ ac.MoveTo(position, time)

  • positon์˜ ์ขŒํ‘œ๋กœ time์‹œ๊ฐ„ ๋™์•ˆ ์›€์ง์ด๊ธฐ

  • sprite๋ฅผ 3์ดˆ๋™์•ˆ (250, 300)์œ„์น˜๋กœ ์›€์ง์—ฌ๋ผ.

sprite.do(ac.MoveTo((250, 300), 3))

๐Ÿ“Œ ac.JumpTo(position, height, jumps, duration)

  • position ์ขŒํ‘œ๋กœ height ๋†’์ด๋งŒํผ duration ๊ฐ„๊ฒฉ์„ ๋‘๊ณ  jumps๋ฒˆ์„ ๋›ฐ์–ด๋ผ

๐Ÿ“Œ ac.RotateTo(Angle, time)

  • time์‹œ๊ฐ„๋™์•ˆ Angle๋งŒํผ ํšŒ์ „ํ•˜๊ธฐ

๐Ÿ“Œ ac.ScaleTo(size, time)

  • time ์‹œ๊ฐ„๋™์•ˆ size๋ฐฐ์ˆ˜๋งŒํผ ํฌ๊ธฐ ๋ฐ”๊พธ๊ธฐ

๐Ÿ“Œ ac.Blink(times, duration)

  • duration ๋™์•ˆ time๋ฒˆ ๊นœ๋ฐ–์ด๊ธฐ

๐Ÿ“Œ ac.FadeIn(time) & ac.FadeOut(time)

  • time์‹œ๊ฐ„๋™์•ˆ Fadein, FadeOutํ•˜๊ธฐ

๐Ÿ“Œ ac.Delay(time)

  • time๋™์•ˆ ๋™์ž‘ํ•˜์ง€ ์•Š๊ณ  ๊ธฐ๋‹ค๋ฆฌ๊ธฐ

  • ex) 2์ดˆ ๋™์•ˆ ๊ธฐ๋‹ค๋ฆฐ ํ›„ (500, 300)์œ„์น˜๋กœ 1์ดˆ ๋™์•ˆ ์›€์ง์ด๊ธฐ

sprite.do(ac.Delay(2)+ ac.MoveTo((500, 300), 1))

๐Ÿ’ป Sample Code

import cocos
import cocos.actions as ac

if __name__ == '__main__':
    cocos.director.director.init(caption='Actions 101')
    layer = cocos.layer.Layer()
    sprite = cocos.sprite.Sprite('tank.png', position=(200, 200))
    sprite.do(ac.MoveTo((250, 300), 3))
    layer.add(sprite)
    scene = cocos.scene.Scene(layer)
    cocos.director.director.run(scene)

Instant actions

๐Ÿ“Œ ac.Place(pos_vec)

  • pos์œ„์น˜๋กœ ์ด๋™

๐Ÿ“Œ ac.Hide() & ac.show()

  • ํ•ด๋‹น sprite์˜ ๋ชจ์Šต์„ ๋ณด์ด๊ฒŒ ํ•˜๊ฑฐ๋‚˜ ๊ฐ€๋ฆฐ๋‹ค

๐Ÿ“Œ ac.ToggleVisibility(bool)

  • ํ•ด๋‹น sprite์˜ ๋ชจ์Šต์„ ๋ณด์ด๊ฒŒ ํ•˜๊ฑฐ๋‚˜ ๊ฐ€๋ฆฐ๋‹ค
  • sprite์„ ๋ณด์ด๊ฒŒ ํ•˜๊ธฐ
    	sprite.do(ac.ToggleVisibility(true))

def func1(): 
    print ("hello")
    action = CallFunc(func1) 
    sprite.do(action) 
    
def func2(sprite): 
    print ("hello") 
    action = CallFuncS(func2) 
    sprite.do(action)

Combining Actions

ac.MoveBy((80, 0), 3) + ac.Delay(3) + ac.CallFunc(sprite.kill) )
ac.MoveBy((80, 0), 3) | ac.RotateBy(90, 2) )
ac.MoveBy((80, 0), 3) | ac.RotateBy(90, 2) + ac.CallFunc(sprite.kill) )

Custom Actions

โœ… duration ๊ฐ„๊ฒฉ์œผ๋กœ sprite์˜ ์ƒ‰์„ ๋ฐ”๊พธ๊ธฐ

class Hit(ac.IntervalAction):

    def init(self, duration=0.5):
        self.duration = duration
        
    def update(self, t):
        self.target.color = (255, 255 * t, 255 * t)

โœ” sprite๋ฅผ 2์ดˆ๋™์•ˆ 3๋ฒˆ ๊นœ๋นก์ด๋ฉฐ 3์ดˆ๋™์•ˆ ์ƒ‰์„ ๋ฐ”๊พผ๋‹ค

sprite.do(ac.Blink(3,2) + Hit(3))
profile
๊ฒŒ์ž„ ๊ฐœ๋ฐœ์ž

0๊ฐœ์˜ ๋Œ“๊ธ€