Ejemplo n.º 1
0
        public MainPage()
        {
            InitializeComponent();

            var cocossharpView = new CocosSharpView();

            cocossharpView.ViewCreated = (sender, e) =>
            {
                var ccgameView = sender as CCGameView;
                var scene      = new CCScene(ccgameView);

                accelerometer         = new CCAccelerometer(ccgameView);
                accelerometer.Enabled = true;

                var listener = new CCEventListenerAccelerometer();
                listener.OnAccelerate = DidAccelerate;
                scene.AddEventListener(listener);

                var callBack = new TimerCallback((o) => { accelerometer.Update(); });
                timer = new Timer(callBack, null, Timeout.Infinite, Timeout.Infinite);
                ccgameView.RunWithScene(scene);
            };
            MyGrid.Children.Add(cocossharpView, 0, 0);

            StartButton.Clicked += (sender, e) => timer.Change(0, 1000);
            StopButton.Clicked  += (sender, e) => timer.Change(Timeout.Infinite, Timeout.Infinite);
        }
Ejemplo n.º 2
0
        public override void OnEnter()
        {
            base.OnEnter();

            CCRect visibleBounds = VisibleBoundsWorldspace;

            var origin = Layer.VisibleBoundsWorldspace.Origin;
            var size   = Layer.VisibleBoundsWorldspace.Size;

            var sprite = new CCSprite(TestResource.s_Ball);

            sprite.Position = origin + size.Center;
            AddChild(sprite);

            // Create our Accelerometer Listener
            var listener = new CCEventListenerAccelerometer();

            if (!Window.Accelerometer.Enabled)
            {
                Window.Accelerometer.Enabled = true;
            }

            // We will use Lambda expressions to attach the event process
            listener.OnAccelerate = (acceleration) => {
                var ballSize = sprite.ContentSize;
                var acc      = acceleration.Acceleration;
                var ptNow    = sprite.Position;

                var orientation = Application.CurrentOrientation;

                //CCLog.Log("Accelerate : X: {0} Y: {1} Z: {2} orientation: {3}", accelerationValue.X, accelerationValue.Y, accelerationValue.Z, orientation );
                if (orientation == CCDisplayOrientation.LandscapeRight || orientation == CCDisplayOrientation.LandscapeLeft)
                {
#if ANDROID
                    ptNow.X += (float)acc.Y * 9.81f;
                    ptNow.Y -= (float)acc.X * 9.81f;
#elif NETFX_CORE || WINDOWS_PHONE8
                    ptNow.X -= (float)acc.Y * 9.81f;
                    ptNow.Y += (float)acc.X * 9.81f;
#elif IOS
                    ptNow.X += (float)acc.Y * 9.81f;
                    ptNow.Y -= (float)acc.X * 9.81f;
#endif
                }
                ptNow.X         = MathHelper.Clamp(ptNow.X, (float)(visibleBounds.Origin.X + ballSize.Width / 2.0), (float)(visibleBounds.Origin.X + visibleBounds.Size.Width - ballSize.Width / 2.0));
                ptNow.Y         = MathHelper.Clamp(ptNow.Y, (float)(visibleBounds.Origin.Y + ballSize.Height / 2.0), (float)(visibleBounds.Origin.Y + visibleBounds.Size.Height - ballSize.Height / 2.0));
                sprite.Position = ptNow;
            };

            // Now we tell the event dispatcher that the sprite is interested in Accelerometer events
            sprite.AddEventListener(listener);
        }
        public void Create(object sender, EventArgs e)
        {
            var ccgameView = sender as CCGameView;
            var scene      = new CCScene(ccgameView);

            accelerometer         = new CCAccelerometer(ccgameView);
            accelerometer.Enabled = true;

            var listener = new CCEventListenerAccelerometer();

            listener.OnAccelerate = DidAccelerate;
            scene.AddEventListener(listener);

            var callBack = new TimerCallback((o) => {
                accelerometer.Update();
            });

            timer = new Timer(callBack, null, Timeout.Infinite, Timeout.Infinite);

            ccgameView.RunWithScene(scene);
        }
Ejemplo n.º 4
0
        protected override void AddedToScene()
        {
            base.AddedToScene();

            // Use the bounds to layout the positioning of our drawable assets
            var bounds = VisibleBoundsWorldspace;

            // Register for touch events
            var touchListener = new CCEventListenerTouchAllAtOnce();

            touchListener.OnTouchesEnded = OnTouchesEnded;

            touchListener.OnTouchesMoved = HandleTouchesMoved;

            var tiltListener = new CCEventListenerAccelerometer();

            tiltListener.IsEnabled    = true;
            tiltListener.OnAccelerate = MovePaddle;
            AddEventListener(touchListener, this);
            AddEventListener(tiltListener, this);
        }
Ejemplo n.º 5
0
        public override void OnEnter()
        {
            base.OnEnter(); CCSize windowSize = Layer.VisibleBoundsWorldspace.Size;

            titleLabel.Position = new CCPoint(windowSize.Width / 2, windowSize.Height - 80);

            if (subtitleLabel != null)
            {
                subtitleLabel.Position = (new CCPoint(windowSize.Width / 2, windowSize.Height - 120));
            }

            ball.Position = windowSize.Center;

            Window.Accelerometer.Enabled = true;

            // Register Touch Event
            var accelListener = new CCEventListenerAccelerometer();

            accelListener.OnAccelerate = DidAccelerate;

            AddEventListener(accelListener);
        }