예제 #1
0
        public override void TouchesBegan(NSSet touches, UIEvent evt)
        {
            // Called when a touch begins

            // control player motion
            base.TouchesBegan(touches, evt);
            var touch       = (UITouch)touches.AnyObject;
            var touchedNode = GetNodeAtPoint(touch.LocationInNode(this));


            Console.WriteLine(touch.LocationInNode(this));
            Console.WriteLine(gameTitle.Position);
            Console.WriteLine(touchedNode);

            //starts game on title tap
            if (touchedNode.Name == "stumblingMan")
            {
                Console.WriteLine("it worked");
                AddChild(gameSong);
                startScreen.RemoveFromParent();
                gameTitle.RemoveFromParent();
                AddChild(playerObject);
                AddChild(pauseButton);

                startGame();
            }
            else if (touchedNode == helpButton)
            {
                if (tappedHelp == false)
                {
                    tappedHelp = true;
                    AddChild(instructions);
                }
                else
                {
                    tappedHelp = false;
                    instructions.RemoveFromParent();
                }
            }
            else if (touchedNode.Name == "gameOver")
            {
                obstacle.Position  = new CGPoint(Frame.Width / 2, Frame.Bottom + obhalf + 5);
                enemy.Position     = new CGPoint(Frame.Width / 2, Frame.Bottom + obhalf + 5);
                obstacle1.Position = new CGPoint(obstacle1.Frame.X + obstacle1.Frame.Width / 2, Frame.Bottom + obhalf + 1);
                obstacle2.Position = new CGPoint(obstacle2.Frame.X + obstacle2.Frame.Width / 2, Frame.Bottom + obhalf + 1);
                obstacle4.Position = new CGPoint(obstacle4.Frame.X + obstacle4.Frame.Width / 2, Frame.Bottom + obhalf + 1);

                playerObject.Paused = true;
                playerObject.RemoveAllActions();
                speed = 0;
                endGame.RemoveFromParent();
                gameSong.RunAction(SKAction.CreatePlay());
                startGame();
            }
            else if (touchedNode == pauseButton)
            {
                gamePause();
            }

            else if (!gameended)
            {
                playerMove(touch);
            }

            void gamePause()
            {
                if (pauseTouched == false)
                {
                    Console.WriteLine("UNSstartpause");

                    // all obstacles must unsubscribe at the begining of a pause
                    if (inprogress)
                    {
                        timer.Elapsed  -= sendobstacle;
                        timer1.Elapsed -= sendobstacle1;
                        timer2.Elapsed -= sendobstacle2;
                        timer3.Elapsed -= sendobstacle3;
                        timer4.Elapsed -= sendobstacle4;
                    }

                    pauseTouched = true;
                    AddChild(pauseOverlay);
                    AddChild(highScores);

                    for (int i = 0; i < 10; i++)
                    {
                        if (i >= 1)
                        {
                            scorelist[i].Text     = String.Format("Score: {0}", scores[i]);
                            scorelist[i].Position = new CGPoint(Frame.Width / 2, scorelist[i - 1].Position.Y - (scorelist[i - 1].Frame.Height / 2) - 42);
                        }
                        else
                        {
                            scorelist[i].Text     = String.Format("Score: {0}", scores[i]);
                            scorelist[i].Position = new CGPoint(Frame.Width / 2, highScores.Position.Y - (highScores.Frame.Height / 2) - 42);
                        }
                        AddChild(scorelist[i]);
                    }
                    Scene.Paused = true;
                }
                else
                {
                    Console.WriteLine("SUBendpause");

                    //all obstacle timers must subscribe at the end of a pause
                    if (inprogress)
                    {
                        timer.Elapsed  += sendobstacle;
                        timer1.Elapsed += sendobstacle1;
                        timer2.Elapsed += sendobstacle2;
                        timer3.Elapsed += sendobstacle3;
                        timer4.Elapsed += sendobstacle4;
                    }

                    pauseOverlay.RemoveFromParent();
                    highScores.RemoveFromParent();
                    for (int i = 0; i < 10; i++)
                    {
                        scorelist[i].RemoveFromParent();
                    }
                    pauseTouched = false;
                    Scene.Paused = false;
                }
            }

            // detects left and right screen taps, moves player accordingly
            void playerMove(UITouch location)
            {
                if (touch.LocationInNode(this).X >= playerObject.Frame.Right)
                {
                    if (DIR)
                    {
                        Console.WriteLine("clickright + moveright" + speed);
                        playerObject.RunAction(SKAction.RepeatActionForever(SKAction.MoveBy(difficulty, 0, 2.0f)));
                        speed = speed + difficulty;
                    }
                    else
                    {
                        Console.WriteLine("clickright + moveleft" + speed);
                        playerObject.RunAction(SKAction.RepeatActionForever(SKAction.MoveBy(difficulty - speed, 0, 2.0f)));
                        speed = difficulty;
                        DIR   = true;
                    }
                }
                if (touch.LocationInNode(this).X < playerObject.Frame.Left)
                {
                    if (!DIR)
                    {
                        Console.WriteLine("clickleft + moveleft" + speed);
                        playerObject.RunAction(SKAction.RepeatActionForever(SKAction.MoveBy(-difficulty, 0, 2.0f)));
                        speed = speed - difficulty;
                    }
                    else
                    {
                        Console.WriteLine("clickleft + moveleft" + speed);
                        playerObject.RunAction(SKAction.RepeatActionForever(SKAction.MoveBy(-difficulty - speed, 0, 2.0f)));
                        speed = -difficulty;
                        DIR   = false;
                    }
                }
            }

            void startGame()
            {
                //setting the game is in progress to true
                playerObject.PhysicsBody.AllowsRotation = false;
                obstacle.PhysicsBody.AllowsRotation     = false;
                enemy.PhysicsBody.AllowsRotation        = false;
                obstacle1.PhysicsBody.AllowsRotation    = false;
                obstacle2.PhysicsBody.AllowsRotation    = false;
                obstacle4.PhysicsBody.AllowsRotation    = false;

                inprogress          = true;
                gameended           = false;
                playerObject.Paused = false;

                #region for timers

                timer          = new System.Timers.Timer();
                timer.Interval = 1000;
                Console.WriteLine("SUB");
                timer.Elapsed  += sendobstacle;
                timer.AutoReset = true;
                timer.Enabled   = true;

                timer1          = new System.Timers.Timer();
                timer1.Interval = 2000;
                Console.WriteLine("SUB");
                timer1.Elapsed  += sendobstacle1;
                timer1.AutoReset = true;
                timer1.Enabled   = true;

                timer2          = new System.Timers.Timer();
                timer2.Interval = 4000;
                Console.WriteLine("SUB");
                timer2.Elapsed  += sendobstacle2;
                timer2.AutoReset = true;
                timer2.Enabled   = true;

                timer3          = new System.Timers.Timer();
                timer3.Interval = 1150;
                Console.WriteLine("SUB");
                timer3.Elapsed  += sendobstacle3;
                timer3.AutoReset = true;
                timer3.Enabled   = true;

                timer4          = new System.Timers.Timer();
                timer4.Interval = 1250;
                Console.WriteLine("SUB");
                timer4.Elapsed  += sendobstacle4;
                timer4.AutoReset = true;
                timer4.Enabled   = true;

                #endregion


                startScreen.RunAction(SKAction.CreatePause());
                Scroll(sidewalk1);
                Scroll(sidewalk2);
                Scroll(sidewalk3);

                playerObject.XScale   = 2.0f;
                playerObject.YScale   = 2.0f;
                playerObject.Position = new CGPoint(Frame.Width / 2, Frame.Height / 4);
                Scroll(road1);
                Scroll(road2);
                Scroll(road3);
                Scroll(road4);
                Scroll(road5);
                Scroll(road6);

                Scroll(building1);
                Scroll(building2);
                Scroll(building3);
                Scroll(building4);
                Scroll(building5);
                Scroll(building6);
                Scroll(building7);
                Scroll(building8);
                Scroll(building9);
                Scroll(building10);
                Scroll(building11);
                Scroll(building12);
                Scroll(building13);
                Scroll(building14);


                animatePlayer();

                playerObject.RunAction(SKAction.RepeatActionForever(SKAction.MoveBy(0.001f, 0, 2.0f)));
                DIR = true;
            }
        }