specific FUNCTION for specific TIME

edited May 2016 in CocosSharp

Hi,

I am new to cocossharp and am experimenting the functionalities of the tool by following up tutorials and suggestions from the Forums.
Now , am building a shooter game and I have implemented some features in the game like collision detection, bullets firing and so on and I have added some sprites (player ship, bullets and enemy ship).

Over here I have a query, like I want to include a feature like "Shield" (for playership) and major specification will be when "Playership" collides with "Bonus Sprite" (will randomly fall from top), the player will get a shield (a shield sprite will appear over playership) functionality and this shield will last for 20 seconds. After 20 seconds the shield sprite should disappear and this functionality should only happen, whenever the player collides with "BONUS SPRITE".

I tried some steps with schedule and unschedule methods and also with removefromparent method, but nothing works , could anyone please provide me with some suggestions to accomplish this.

Thanks

Tagged:

Posts

  • IlledanIlledan NOMember ✭✭

    I used ScheduleOnce for my effect:

    Example:

            var flower = new CCParticleFlower (new CCPoint (0, 0), CCEmitterMode.Gravity);
            ...
            AddChild (flower);
            ScheduleOnce ((dt) => RemoveChild (flower), 6.0f);
            ...
    
  • Thanks IIIedan, that was helpful. There is one another thing am confused about, I have a boolean variable "MyShield" and it becomes "TRUE" during the runtime depending upon the player. When it becomes "TRUE" I will add a method called "addMyshield()" and in that method I will add "particlegalaxy" as shield to player and make it disappear after sometime . please see the code below

    public IntroLayer()
    : base(CCColor4B.Black)
    {
    visibleShips = new List();

            addmyShip();
            addAlienShip();
    
            Schedule(t =>
            {
    
                visibleShips.Add(AddBullets());
    
            }, 0.5f);
    
           if(MyShield)
                    addMyshield()    
        }
    
        void addMyshield()   
        {
    
    
            shield = new CCParticleGalaxy(playerShip.Position);
    
            shield.Scale = 1.0f;
    
            shield.Position = playerShip.Position;
    
            AddChild(shield);
    
            ScheduleOnce((t) => RemoveChild(shield), 20.0f);
    
        }
    

    My question over here is, whenever the value "MyShield" becomes TRUE during runtime, the "addMyShield" method is not getting executed and am unable to addchild(CCParticlegalaxy) during runtime. From the execution I can see that I have to initialize the method when the code begins in INTROLAYER, only then it appears but if I try to addchild during runtime it's not happening .Where exactly am making mistake?

  • IlledanIlledan NOMember ✭✭
    edited May 2016

    You need to move your check inside the Schedule, else it's only checked when you are initializing this object. Also remember to set MyShield to false when it's "used".

    public IntroLayer() : base(CCColor4B.Black)
    {
            visibleShips = new List();
            addmyShip();
            addAlienShip();
    
            Schedule(t =>
            {
                visibleShips.Add(AddBullets());
                if(MyShield)
                    addMyshield()    
            }, 0.5f);   
        }
    
        void addMyshield()   
        {
            MyShield = false;
            shield = new CCParticleGalaxy(playerShip.Position);
            shield.Scale = 1.0f;
            //shield.Position = playerShip.Position; // You allready specified the position in the CCParticleGalaxy constructor.
            AddChild(shield);
            ScheduleOnce((t) => RemoveChild(shield), 20.0f);
        }
    
    
  • Thanks again for the explanation , that was clear and concise.
    But, if we use multiple "Schedule" statements, wouldn't that cause a performance drop ?

  • IlledanIlledan NOMember ✭✭

    Schedule source :
    https://github.com/mono/CocosSharp/blob/9346063d635b196be683a0e1796b32c4db0acc95/src/Events/CCScheduler.cs

    can't see anything indicating a performance drop when using multiple Schedule statements.

    If you like a solution without double Schedule: (Though, this is not testable)

        async void addMyshield()   
        {
            MyShield = false;
            shield = new CCParticleGalaxy(playerShip.Position);
            shield.Scale = 1.0f;
            AddChild(shield);
            await Task.Delay(20000); 
            RemoveChild(shield);
        }
    

    Also, ScheduleOnce will pause the call when phone is locked, making it continue when app has focus again.

  • Thanks Illedan for your guidance, that was highly helpful.

Sign In or Register to comment.