Пример #1
0
		public StopPropagationTest() : base()
		{
			var touchOneByOneListener = new CCEventListenerTouchOneByOne();
			touchOneByOneListener.IsSwallowTouches = true;

			touchOneByOneListener.OnTouchBegan = (touch, touchEvent) =>
			{
				// Skip if don't touch top half screen.
				if (!IsPointInTopHalfAreaOfScreen(touch.LocationOnScreen))
					return false;

				var target = (CCSprite)touchEvent.CurrentTarget;
				Debug.Assert (target.Tag == TAG_BLUE_SPRITE, "Yellow blocks shouldn't response event.");

				if (IsPointInNode(touch.LocationOnScreen, target))
				{
					target.Opacity = 180;
					return true;
				}

				// Stop propagation, so yellow blocks will not be able to receive event.
				touchEvent.StopPropogation();
				return false;
			};

			touchOneByOneListener.OnTouchEnded = (touch, touchEvent) => 
			{
				var target = (CCSprite)touchEvent.CurrentTarget;
				target.Opacity = 255;
			};

			var touchAllAtOnceListener = new CCEventListenerTouchAllAtOnce();
			touchAllAtOnceListener.OnTouchesBegan = (touches, touchEvent) => 
			{
				// Skip if don't touch top half screen.
				if (IsPointInTopHalfAreaOfScreen(touches[0].LocationOnScreen))
					return;

				var target = (CCSprite)touchEvent.CurrentTarget;
				Debug.Assert(target.Tag == TAG_BLUE_SPRITE2, "Yellow blocks shouldn't response event.");

				if (IsPointInNode(touches[0].LocationOnScreen, target))
				{
					target.Opacity = 180;
				}
				// Stop propagation, so yellow blocks will not be able to receive event.
				touchEvent.StopPropogation();
			};

			touchAllAtOnceListener.OnTouchesEnded = (touches, touchEvent) => 
			{
				// Skip if don't touch top half screen.
				if (IsPointInTopHalfAreaOfScreen(touches[0].LocationOnScreen))
					return;

				var target = (CCSprite)touchEvent.CurrentTarget;
				Debug.Assert(target.Tag == TAG_BLUE_SPRITE2, "Yellow blocks shouldn't response event.");

				if (IsPointInNode(touches[0].LocationOnScreen, target))
				{
					target.Opacity = 255;;
				}
				// Stop propagation, so yellow blocks will not be able to receive event.
				touchEvent.StopPropogation();
			};

			var keyboardEventListener = new CCEventListenerKeyboard();

			keyboardEventListener.OnKeyPressed = (keyboardEvent) => 
			{
				var target = (CCSprite)keyboardEvent.CurrentTarget;
				Debug.Assert(target.Tag == TAG_BLUE_SPRITE || target.Tag == TAG_BLUE_SPRITE2, "Yellow blocks shouldn't response event.");
				// Stop propagation, so yellow blocks will not be able to receive event.
				keyboardEvent.StopPropogation();
			};


			const int SPRITE_COUNT = 8;

			for (int i = 0; i < SPRITE_COUNT; i++)
			{
				CCSprite sprite;
				CCSprite sprite2;

				if(i==4)
				{
					sprite = new CCSprite("Images/CyanSquare.png");
					sprite.Tag = TAG_BLUE_SPRITE;
					AddChild(sprite, 100);

					sprite2 = new CCSprite("Images/CyanSquare.png");
					sprite2.Tag = TAG_BLUE_SPRITE2;
					AddChild(sprite2, 100);
				}
				else
				{
					sprite = new CCSprite("Images/YellowSquare.png");
					AddChild(sprite, 0);
					sprite2 = new CCSprite("Images/YellowSquare.png");
					AddChild(sprite2, 0);
				}

				sprite.AddEventListener(touchOneByOneListener.Copy());
				sprite.AddEventListener(keyboardEventListener.Copy());

				sprite2.AddEventListener(touchAllAtOnceListener.Copy());
				sprite2.AddEventListener(keyboardEventListener.Copy());


				var visibleBounds = Layer.VisibleBoundsWorldspace;
                sprite.Position = new CCPoint( visibleBounds.Origin.X + visibleBounds.Size.Width / (SPRITE_COUNT - 1) * i, visibleBounds.Center.Y + sprite2.ContentSize.Height/2 +10);
                sprite2.Position = new CCPoint( visibleBounds.Origin.X + visibleBounds.Size.Width / (SPRITE_COUNT - 1) * i, visibleBounds.Center.Y - sprite2.ContentSize.Height/2-10);
			}

		}
Пример #2
0
		public override void OnEnter ()
		{
			base.OnEnter ();

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

			var sprite1 = new CCSprite("Images/CyanSquare.png");
			sprite1.Position = origin + new CCPoint (size.Width / 2, size.Height / 2) + new CCPoint (-80, 80);
			AddChild(sprite1, 10);

			var sprite2 = new CCSprite("Images/MagentaSquare.png");
			sprite2.Position = origin + new CCPoint (size.Width / 2, size.Height / 2);
			AddChild(sprite2, 20);

			var sprite3 = new CCSprite("Images/YellowSquare.png");
			sprite3.Position = CCPoint.Zero;
			sprite2.AddChild(sprite3, 1);

			// Make sprite1 touchable
			var listener1 = new CCEventListenerTouchOneByOne ();
			listener1.IsSwallowTouches = true;

			listener1.OnTouchBegan = (touch, touchEvent) => 
			{
				var target = (CCSprite)touchEvent.CurrentTarget;

				var locationInNode = target.Layer.ScreenToWorldspace(touch.LocationOnScreen);
				var s = target.ContentSize;
                CCRect rect = target.BoundingBoxTransformedToWorld;

				if (rect.ContainsPoint(locationInNode))
				{
					CCLog.Log("sprite began... x = {0}, y = {1}", locationInNode.X, locationInNode.Y);
					target.Opacity = 180;
					return true;
				}
				return false;
			};

			listener1.OnTouchMoved = (touch, touchEvent) => 
			{
				var target = (CCSprite)touchEvent.CurrentTarget;
                CCPoint pt = touch.PreviousLocationOnScreen + touch.Delta;
                target.Position = target.WorldToParentspace(Layer.ScreenToWorldspace(pt));
			};

			listener1.OnTouchEnded = (touch, touchEvent) => 
			{
				var target = (CCSprite)touchEvent.CurrentTarget;
				CCLog.Log("sprite onTouchesEnded..");
				target.Opacity = 255;
				if (target == sprite2)
				{
					sprite1.LocalZOrder = 100;
				}
				else if(target == sprite1)
				{
					sprite1.LocalZOrder = 0;
				}
			};


            sprite1.AddEventListener(listener1);
            sprite2.AddEventListener(listener1.Copy());
            sprite3.AddEventListener(listener1.Copy());


			var removeAllTouchItem = new CCMenuItemFont("Remove All Touch Listeners", (sender) => {
				var senderItem = (CCMenuItemFont)sender;
				senderItem.LabelTTF.Text = "Only Next item could be clicked";

                RemoveEventListeners(CCEventListenerType.TOUCH_ONE_BY_ONE);

				var nextItem = new CCMenuItemFont("Next", (senderNext) => NextCallback(senderNext));
			

				CCMenuItemFont.FontSize = 16;
                nextItem.Position =  new CCPoint(visibleBounds.Origin.X + visibleBounds.Size.Width -100, 100);

				var menu2 = new CCMenu(nextItem);
				menu2.Position = CCPoint.Zero;
				menu2.AnchorPoint = CCPoint.AnchorLowerLeft;
				this.AddChild(menu2);
			});

			CCMenuItemFont.FontSize = 16;
            removeAllTouchItem.Position = new CCPoint(visibleBounds.Origin.X + visibleBounds.Size.Width -100, 80);

			var menu = new CCMenu(removeAllTouchItem);
			menu.Position = CCPoint.Zero;
			menu.AnchorPoint = CCPoint.AnchorLowerLeft;
			AddChild(menu);


		}
Пример #3
0
		public override void OnEnter()
		{
            base.OnEnter(); 

            int i;

            for (i = 0; i < SPRITE_COUNT; i++)
            {
                CCSprite sprite;

                if(i==4)
                {
                    sprite = new CCSprite("Images/CyanSquare.png") { Tag = TAG_SPRITE + i};
                    blueSprite = sprite;
                    //blueSprite.Scene = Scene;
                    blueSprite.GlobalZOrder = -1;

                }
                else
                {
                    sprite = new CCSprite("Images/YellowSquare.png") { Tag = TAG_SPRITE + i};
                }

                // For right now since we do not draw by GlobalZOrder we will move a value to the
                // Local z-order so we can fake this.  It is a Hack for now for the foreground
                // to background movement.
                AddChild(sprite, 2);

            }

            CCRect visibleRect = Layer.VisibleBoundsWorldspace;

			var listener = new CCEventListenerTouchOneByOne();
			listener.IsSwallowTouches = true;

			listener.OnTouchBegan = (touch, touchEvent) =>
				{
					var target = (CCSprite)touchEvent.CurrentTarget;

					var locationInNode = target.Layer.ScreenToWorldspace(touch.LocationOnScreen);
					var s = target.ContentSize;
                    var rect = target.BoundingBoxTransformedToWorld;
		
					if (rect.ContainsPoint(locationInNode))
					{
						CCLog.Log("sprite began... x = {0}, y = {1}", locationInNode.X, locationInNode.Y);
						target.Opacity = 180;
						return true;
					}
					return false;

				};

			listener.OnTouchMoved = (touch, touchEvent) =>
			{
				var target = (CCSprite)touchEvent.CurrentTarget;
				target.Position = target.Position + touch.Delta;
			};

			listener.OnTouchEnded = (touch, touchEvent) => 
				{
					var target = (CCSprite)touchEvent.CurrentTarget;
					CCLog.Log ("sprite OnTouchEnded...");
					target.Opacity = 255;
				};

			i = 0;
			foreach (var child in Children)
			{
				if (child.Tag >= TAG_SPRITE && child.Tag <= TAG_SPRITE_END)
				{
                    child.Position = new CCPoint(visibleRect.Origin.X + visibleRect.Size.Width / (SPRITE_COUNT - 1) * i, visibleRect.Center.Y);
					i++;
					child.AddEventListener(listener.Copy());
				}
			}

			Schedule();

		}