locked
How can I remove a child?

    Question

  • Hi,
    Why I can't remove a child?

     bool fish82Tapped = false;
     Rectangle fish82 = new Rectangle();

     public  MainPage()
            {

                this.InitializeComponent();
                this.navigationHelper = new NavigationHelper(this);
                this.navigationHelper.LoadState += navigationHelper_LoadState;
                this.navigationHelper.SaveState += navigationHelper_SaveState;
             
               
                loadFishes();

               this.fish82.Tapped += new TappedEventHandler(fish82_Tapped);
                 }

            private void loadFishes()
     {
          
                load82(250.0, 250.0);

     }
       private void load82(double left7, double top7)
            {
                fish82 = new Rectangle();

                fish82.Width = 177; // 122; // 160; // 104;
                fish82.Height = 63; // 90; // 104; // 74;

                // Create an ImageBrush
                ImageBrush imgBrush82 = new ImageBrush();

                BitmapImage img82 = new BitmapImage();
              //  img82.UriSource = new Uri(this.BaseUri, "ms-appx:///Images/821.png");
              //  img82.UriSource = new Uri(this.BaseUri, "ms-appx:///Images/82.png");
            //    img82.UriSource = new Uri(this.BaseUri, "ms-appx:///Images/3orange.png");
                img82.UriSource = new Uri(this.BaseUri, "ms-appx:///Images/animtrout.png");


                imgBrush82.ImageSource = img82;
                imgBrush82.Stretch = Stretch.None;
                imgBrush82.AlignmentX = AlignmentX.Left;
                imgBrush82.AlignmentY = AlignmentY.Top;

                SpriteSheetOffset82 = new TranslateTransform();
                SpriteSheetOffset82.X = 0;
                SpriteSheetOffset82.Y = 0;

                imgBrush82.Transform = SpriteSheetOffset82;

                fish82.Fill = imgBrush82;

                myCanvas1.Children.Add(fish82);

                Canvas.SetLeft(fish82, left7); //200.0);
                Canvas.SetTop(fish82, top7);  //80.0);
              fish82.Tapped += new TappedEventHandler(fish82_Tapped);


            }   private void load82(double left7, double top7)
            {
                fish82 = new Rectangle();

                fish82.Width = 177;
                fish82.Height = 63;

                ImageBrush imgBrush82 = new ImageBrush();

                BitmapImage img82 = new BitmapImage();
                img82.UriSource = new Uri(this.BaseUri, "ms-appx:///Images/animtrout.png");

                imgBrush82.ImageSource = img82;
                imgBrush82.Stretch = Stretch.None;
                imgBrush82.AlignmentX = AlignmentX.Left;
                imgBrush82.AlignmentY = AlignmentY.Top;

                SpriteSheetOffset82 = new TranslateTransform();
                SpriteSheetOffset82.X = 0;
                SpriteSheetOffset82.Y = 0;

                imgBrush82.Transform = SpriteSheetOffset82;

                fish82.Fill = imgBrush82;

                myCanvas1.Children.Add(fish82);

                Canvas.SetLeft(fish82, left7);
                Canvas.SetTop(fish82, top7);
              
                fish82.Tapped += new TappedEventHandler(fish82_Tapped);


            }
     private void fish82_Tapped(object sender, TappedRoutedEventArgs e)
            {
                fish82Tapped = true;
                txtScore.Text = (5 + System.Convert.ToInt32(txtScore.Text)).ToString();
                txtHits.Text = (1 + System.Convert.ToInt32(txtHits.Text)).ToString();
               fish82.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
               myCanvas1.Children.Remove(fish82);
               myCanvas1.UpdateLayout();
               fish82.UpdateLayout();
            }

       private void OnUpdate(object sender, object e)
            {

                if (fish82Tapped == true)
                {
                    fish82.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    myCanvas1.Children.Remove(fish82);
                    myCanvas1.UpdateLayout();
                    fish82.UpdateLayout(); ;

                }
                else
                {
                    this.timeTillNextFrame82 += TimeSpan.FromSeconds(1 / 60f);
                    if (this.timeTillNextFrame82 > TimePerFrame82)
                    {
                        this.currentFrame82 = (this.currentFrame82 + 1 + NumberOfFrames82) % NumberOfFrames82;
                        var column = this.currentFrame82 % NumberOfColumns82;
                        var row = this.currentFrame82 / NumberOfColumns82;

                        this.SpriteSheetOffset82.X = -column * FrameWidth82;
                        this.SpriteSheetOffset82.Y = -row * FrameHeight82;
                    } timeTillNextFrame82 = TimeSpan.Zero;

                }
    }

     protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                navigationHelper.OnNavigatedTo(e);
               
                CompositionTarget.Rendering += this.OnUpdate;
            }
       
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                navigationHelper.OnNavigatedFrom(e);
                CompositionTarget.Rendering -= this.OnUpdat

    }

    Thanks


    ADRIAN DIBU

    Wednesday, October 29, 2014 12:04 AM

Answers

  • Hi,

    For who is interested the solution is

    Link

    https://onedrive.live.com/redir?resid=857BC62170EDDB22!4152&authkey=!ADLtQMYzWIQ0oB0&ithint=file%2czip

        if (fish82Tapped == true)
                {
                //    fish82.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    myCanvas1.Children.Remove(fish82);
                    myCanvas1.UpdateLayout();

                    obj82 = new Rect();
                    obj82.Height = 1;
                    obj82.Width = 1;
                    obj82.X = 100.0; // Canvas.GetLeft(fish82);
                    obj82.Y = 100.0; // Canvas.GetTop(fish82);
                }
                else
                {
                    this.timeTillNextFrame82 += TimeSpan.FromSeconds(1 / 60f);
                    if (this.timeTillNextFrame82 > TimePerFrame82)
                    {
                        this.currentFrame82 = (this.currentFrame82 + 1 + NumberOfFrames82) % NumberOfFrames82;
                        var column = this.currentFrame82 % NumberOfColumns82;
                        var row = this.currentFrame82 / NumberOfColumns82;

                        this.SpriteSheetOffset82.X = -column * FrameWidth82;
                        this.SpriteSheetOffset82.Y = -row * FrameHeight82;
                        timeTillNextFrame82 = TimeSpan.Zero;
                    }
                obj82 = new Rect();
                obj82.X = Canvas.GetLeft(fish82);
                obj82.Y = Canvas.GetTop(fish82);
                obj82.Height = 50; // fish82.Height;
                obj82.Width = 150; // fish82.Width;

                obj82.Intersect(obj104);

                if (!obj82.IsEmpty)
                {
                    myCanvas1.Children.Remove(fish82);
                    myCanvas1.UpdateLayout();

                    obj82.Height = 1;
                    obj82.Width = 1;
                    obj82.X = 100.0;
                    obj82.Y = 100.0;
                    missed82 = 1;
                    missedTotal = missed15 + missed70 + missed71 + missed74 + missed80 + missed81 + missed82 + missed101 + missed102;

                    txtMissed.Text = missedTotal.ToString();
                 
                    checkFail();
                }
       
        
                }


    ADRIAN DIBU

    • Marked as answer by adibu Monday, November 17, 2014 3:46 PM
    Monday, November 17, 2014 3:46 PM

All replies

  • Hi Adrian,

    I used your code to create a project and tested, the app added the Rectangle and removed it when I tapped on it. I could not repro your problem, I used the following code snippets.

    public sealed partial class MainPage : Page
        {
            private Rectangle fish82;
            private TranslateTransform SpriteSheetOffset82;
            public MainPage()
            {
                this.InitializeComponent();
                load82(250.0, 250.0);
                this.fish82.Tapped += new TappedEventHandler(fish82_Tapped);
            }
            private void load82(double left7, double top7)
            {
                // the same as yours
            }
            private void fish82_Tapped(object sender, TappedRoutedEventArgs e)
            {
                myCanvas1.Children.Remove(fish82);
            }
    }
    

    Can you try this code in a new project and let me know the result? It’s working on my side.

    If you still have questions about this problem, please send me a repro project. Use your OneDrive and share a link here.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, October 29, 2014 2:29 AM
    Moderator
  • Hi,

    Thanks for the prompt answer.

    Not working.

    This is the link of the project.

    https://onedrive.live.com/redir?resid=857BC62170EDDB22!4052&authkey=!AKfr-l4F1kun3R8&ithint=file%2czip

    When I tap.
    This lines work.

    fish82Tapped = true;
    txtScore.Text = (5 + System.Convert.ToInt32(txtScore.Text)).ToString();
    txtHits.Text = (1 + System.Convert.ToInt32(txtHits.Text)).ToString();
     
    The UI lines don't work. What I am missing?
    fish82.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
    myCanvas1.Children.Remove(fish82);
    myCanvas1.UpdateLayout();
    fish82.UpdateLayout(); 

    If I don't make fish82 from code and is in XMAL it works.  

    Thanks


    ADRIAN DIBU

    Wednesday, October 29, 2014 1:33 PM
  • Hi,

    I fixed the problems.

    Now the fish104 is moving over myCanvas1 limits on axe Y

        if (fish104PositionX > (768 - fish104.Width)|| fish104PositionX < 0)
                {
                    this.fish104VelocityX *= -1;
                }

                if (fish104PositionY > (myCanvas1.Height - (fish104.Height + FloorR768.Height)) || fish104PositionY < 0)
                {
                    this.fish104VelocityY *= -1; // On this axe not working
                }

    When fish104 intersects I expected to have only one Missed, instead I have hundreds

     //   obj104 = new Rect();
                obj104.X = Canvas.GetLeft(fish104);
                obj104.Y = Canvas.GetTop(fish104);  // +canvasShift;
                obj104.Height = fish104.Height;
                obj104.Width = fish104.Width;

            //  obj15 = new Rect();
                obj15.X = Canvas.GetLeft(fish15);
                obj15.Y = Canvas.GetTop(fish15);
                obj15.Height = fish15.Height;
                obj15.Width = fish15.Width;

                obj15.Intersect(obj104);

                if (!obj15.IsEmpty)
                {
                        myCanvas1.Children.Remove(fish15);
                   txtMissed.Text = (1 + System.Convert.ToInt32(txtMissed.Text)).ToString();
                }

    The new link is:

    https://onedrive.live.com/redir?resid=857BC62170EDDB22!4053&authkey=!AGtjwDX_oneh9lI&ithint=file%2czip

    Please run the new to see what happen.

    Thanks

     


    ADRIAN DIBU

    Wednesday, October 29, 2014 9:40 PM
  • Hi,

     New Link:
    https://onedrive.live.com/redir?resid=857BC62170EDDB22!4054&authkey=!AETJ7-H0ZZcUZzc&ithint=file%2czip

     

    When fish104 intersects fish82 I expected to have only one Missed, instead I have hundreds (135). How can I prevent this. I need the missed  for Game messages.

    //  obj82 = new Rect();
                obj82.X = Canvas.GetLeft(fish82);
                obj82.Y = Canvas.GetTop(fish82);  // +canvasShift;
                obj82.Height = fish82.Height;
                obj82.Width = fish82.Width;

                obj82.Intersect(obj104);

                if (!obj82.IsEmpty)
                {
                    myCanvas1.Children.Remove(fish82);
                    txtMissed.Text = (1 + System.Convert.ToInt32(txtMissed.Text)).ToString();
                }

            Thanks


    ADRIAN DIBU

    Friday, October 31, 2014 2:54 PM
  • Hi,

    New Link

    https://onedrive.live.com/redir?resid=857BC62170EDDB22!4055&authkey=!AAPuus3F57ZBIFA&ithint=file%2czip

    What I can understand is when obj104 intersects obj82 two things happens:
           myCanvas1.Children.Remove(fish82)
    and    txtMissed.Text = (1 + System.Convert.ToInt32(txtMissed.Text)).ToString();
    What I got was txtMissed.Text = 135
    and even I renmoved fish82 when it moves back
    it acts like fish82 exist and continue to count
    I got was txtMissed.Text = 270.

    What I expected was txtMissed.Text = 1.

                obj82 = new Rect();
                obj82.X = Canvas.GetLeft(fish82);
                obj82.Y = Canvas.GetTop(fish82); 
                obj82.Height = fish82.Height;
                obj82.Width = fish82.Width;

                obj82.Intersect(obj104);
         
              //  obj82.Intersect(obj204);

                if (!obj82.IsEmpty)
                {
                    myCanvas1.Children.Remove(fish82);
                    txtMissed.Text = (1 + System.Convert.ToInt32(txtMissed.Text)).ToString();
                }
               
                obj82S = new Rect();
                obj82S.X = Canvas.GetLeft(fish82);
                obj82S.Y = Canvas.GetTop(fish82); 
                obj82S.Height = fish82.Height;
                obj82S.Width = fish82.Width;

                obj82S.Intersect(obj204S);

                if (!obj82S.IsEmpty)
                {
                    myCanvas1.Children.Remove(fish82);
                    txtMissed.Text = (1 + System.Convert.ToInt32(txtMissed.Text)).ToString();
                }

    Please try to help me.

    Thanks


    ADRIAN DIBU

    Saturday, November 01, 2014 3:49 PM
  • Hi,
    My problem is:

     private void load82(double left7, double top7)
            {
                fish82 = new Rectangle();

                fish82.Width = 177;
                fish82.Height = 63;

                // Create an ImageBrush
                ImageBrush imgBrush82 = new ImageBrush();

                BitmapImage img82 = new BitmapImage();
                img82.UriSource = new Uri(this.BaseUri, "ms-appx:///Images/animtrout.png");

                imgBrush82.ImageSource = img82;
                imgBrush82.Stretch = Stretch.None;
                imgBrush82.AlignmentX = AlignmentX.Left;
                imgBrush82.AlignmentY = AlignmentY.Top;

                SpriteSheetOffset82 = new TranslateTransform();
                SpriteSheetOffset82.X = 0;
                SpriteSheetOffset82.Y = 0;

                imgBrush82.Transform = SpriteSheetOffset82;

                fish82.Fill = imgBrush82;
                fish82.Tapped += new TappedEventHandler(fish82_Tapped);

                myCanvas1.Children.Add(fish82);

                Canvas.SetLeft(fish82, left7); //200.0);
                Canvas.SetTop(fish82, top7);  //80.0);
            }

    I created a rect obj82 associated to fish82 to check the intersection
            
                obj82 = new Rect();
                obj82.X = Canvas.GetLeft(fish82);
                obj82.Y = Canvas.GetTop(fish82); 
                obj82.Height = fish82.Height;
                obj82.Width = fish82.Width;

                obj82.Intersect(obj104);


                if (!obj82.IsEmpty)
                {
                    myCanvas1.Children.Remove(fish82);
                    txtMissed.Text = (1 + System.Convert.ToInt32(txtMissed.Text)).ToString();
     
                }

    Now when they intersecs I removed the fish82 but not obj82 to have only 1 not 135.
    How can I remove obj82 after first hit to stop counting?

           


    ADRIAN DIBU


    • Edited by adibu Saturday, November 08, 2014 5:36 PM Error
    Saturday, November 08, 2014 5:35 PM
  •  Hi,

    I tried:

    obj82 = new Rect();
                obj82.X = Canvas.GetLeft(fish82);
                obj82.Y = Canvas.GetTop(fish82); 
                obj82.Height = fish82.Height;
                obj82.Width = fish82.Width;

                obj82.Intersect(obj104);

           

                if (!obj82.IsEmpty)
                {
                    myCanvas1.Children.Remove(fish82);
                    txtMissed.Text = (1 + System.Convert.ToInt32(txtMissed.Text)).ToString();

                   obj82 = new Rect();    

                 obj82.Height = 0;
                obj82.Width = 0; 

                }

    I suppose don't have intersection

    Thanks


    ADRIAN DIBU

    Saturday, November 15, 2014 1:24 AM
  • Hi,

     J tried  to make the dimension 0 and I moved it t0 X=0.0 Y=0.0 and obj stil exists and I

    have collisions why?

    link

    https://onedrive.live.com/redir?resid=857BC62170EDDB22!4145&authkey=!APBRgJt2Pu4xbSQ&ithint=file%2czip

    obj82 = new Rect();
                 obj82.X = Canvas.GetLeft(fish82);
                obj82.Y = Canvas.GetTop(fish82);
                obj82.Height = 50; // fish82.Height;
                obj82.Width = 150; // fish82.Width;

                obj82.Intersect(obj104);

                if (!obj82.IsEmpty)
                {
                    myCanvas1.Children.Remove(fish82);
                    obj82.Height = 0;
                    obj82.Width = 0;
                    obj82.X = 0.0;
                    obj82.Y = 0.0;
                    missed82 = 1;
                    missedTotal = missed15 + missed70 + missed71 + missed74 + missed80 + missed81 + missed82 + missed101 + missed102;

                    txtMissed.Text = missedTotal.ToString();

                    checkFail();
                }

      private void fish82_Tapped(object sender, TappedRoutedEventArgs e)
            {
                fish82Tapped = true;
                hit82 = 1;
                hitTotal = hit15 + hit70 + hit71 + hit74 + hit80 + hit81 + hit82 + hit101 + hit102;
                txtHits.Text = hitTotal.ToString();

                txtScore.Text = (5 * hitTotal).ToString();

               myCanvas1.Children.Remove(fish82);
               obj82.Width = 0;
               obj82.Height = 0;
               obj82.X = 0.0;
               obj82.Y = 0.0;
               myCanvas1.UpdateLayout();
               fish82.UpdateLayout();
               checkFail(); 

            }


    ADRIAN DIBU


    • Edited by adibu Sunday, November 16, 2014 1:19 AM Updated link
    Saturday, November 15, 2014 11:56 PM
  • Hi,

     J tried  to make the dimension 1 and I moved it t0 X=100.0 Y=100.0 and obj still exists and I

    have collisions why?

    private void OnUpdate(object sender, object e)
            {
    if (fish82Tapped == true)
                {
                //    fish82.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    myCanvas1.Children.Remove(fish82);
                    obj82 = new Rect();
                    obj82.Height = 1;
                    obj82.Width = 1;
                    obj82.X = 100.0; // Canvas.GetLeft(fish82);
                    obj82.Y = 100.0; // Canvas.GetTop(fish82);

                    myCanvas1.UpdateLayout();
                    fish82.UpdateLayout();

                }
                else
                {
                    this.timeTillNextFrame82 += TimeSpan.FromSeconds(1 / 60f);
                    if (this.timeTillNextFrame82 > TimePerFrame82)
                    {
                        this.currentFrame82 = (this.currentFrame82 + 1 + NumberOfFrames82) % NumberOfFrames82;
                        var column = this.currentFrame82 % NumberOfColumns82;
                        var row = this.currentFrame82 / NumberOfColumns82;

                        this.SpriteSheetOffset82.X = -column * FrameWidth82;
                        this.SpriteSheetOffset82.Y = -row * FrameHeight82;
                        timeTillNextFrame82 = TimeSpan.Zero;
                    }
                }


                obj82 = new Rect();
                obj82.X = Canvas.GetLeft(fish82);
                obj82.Y = Canvas.GetTop(fish82);
                obj82.Height = 50; // fish82.Height;
                obj82.Width = 150; // fish82.Width;

                obj82.Intersect(obj104);

                if (!obj82.IsEmpty)
                {
                    myCanvas1.Children.Remove(fish82);
                    obj82.Height = 0;
                    obj82.Width = 0;
                    obj82.X = 0.0;
                    obj82.Y = 0.0;
                    missed82 = 1;
                    missedTotal = missed15 + missed70 + missed71 + missed74 + missed80 + missed81 + missed82 + missed101 + missed102;

                    txtMissed.Text = missedTotal.ToString();

                    checkFail();
                }
        }




      private void fish82_Tapped(object sender, TappedRoutedEventArgs e)
            {
                fish82Tapped = true;
                hit82 = 1;
                hitTotal = hit15 + hit70 + hit71 + hit74 + hit80 + hit81 + hit82 + hit101 + hit102;
                txtHits.Text = hitTotal.ToString();

                txtScore.Text = (5 * hitTotal).ToString();

            
                checkFail(); 

            }


    ADRIAN DIBU

    Sunday, November 16, 2014 11:34 PM
  • Hi,

    For who is interested the solution is

    Link

    https://onedrive.live.com/redir?resid=857BC62170EDDB22!4152&authkey=!ADLtQMYzWIQ0oB0&ithint=file%2czip

        if (fish82Tapped == true)
                {
                //    fish82.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                    myCanvas1.Children.Remove(fish82);
                    myCanvas1.UpdateLayout();

                    obj82 = new Rect();
                    obj82.Height = 1;
                    obj82.Width = 1;
                    obj82.X = 100.0; // Canvas.GetLeft(fish82);
                    obj82.Y = 100.0; // Canvas.GetTop(fish82);
                }
                else
                {
                    this.timeTillNextFrame82 += TimeSpan.FromSeconds(1 / 60f);
                    if (this.timeTillNextFrame82 > TimePerFrame82)
                    {
                        this.currentFrame82 = (this.currentFrame82 + 1 + NumberOfFrames82) % NumberOfFrames82;
                        var column = this.currentFrame82 % NumberOfColumns82;
                        var row = this.currentFrame82 / NumberOfColumns82;

                        this.SpriteSheetOffset82.X = -column * FrameWidth82;
                        this.SpriteSheetOffset82.Y = -row * FrameHeight82;
                        timeTillNextFrame82 = TimeSpan.Zero;
                    }
                obj82 = new Rect();
                obj82.X = Canvas.GetLeft(fish82);
                obj82.Y = Canvas.GetTop(fish82);
                obj82.Height = 50; // fish82.Height;
                obj82.Width = 150; // fish82.Width;

                obj82.Intersect(obj104);

                if (!obj82.IsEmpty)
                {
                    myCanvas1.Children.Remove(fish82);
                    myCanvas1.UpdateLayout();

                    obj82.Height = 1;
                    obj82.Width = 1;
                    obj82.X = 100.0;
                    obj82.Y = 100.0;
                    missed82 = 1;
                    missedTotal = missed15 + missed70 + missed71 + missed74 + missed80 + missed81 + missed82 + missed101 + missed102;

                    txtMissed.Text = missedTotal.ToString();
                 
                    checkFail();
                }
       
        
                }


    ADRIAN DIBU

    • Marked as answer by adibu Monday, November 17, 2014 3:46 PM
    Monday, November 17, 2014 3:46 PM