开发者

Silverlight - game collision objects

I am developing simple catching application in Silverlight (in Blend 4). Do you know how to simply solve object collision of falling objects from the top to the bottom into the white rectangle? Image - http://img94.imageshack.us/img94开发者_开发百科/8200/svlk.jpg

Thanks

current code:

public partial class MainPage : UserControl
{ 
    int skore=0;

    public MainPage()
    {
        InitializeComponent();
        chytac.Visibility=Visibility.Collapsed;
    }

    private void UserControl_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        String k=e.Key.ToString();
        klavesa.Content=k;
        if(e.Key.ToString().Equals("Left")){
            double x=Canvas.GetLeft(chytac);
            x=x-20;
            if(x>0){
                Canvas.SetLeft(chytac,x);
            }   

        }
        if(e.Key.ToString().Equals("Right")){
            double x=Canvas.GetLeft(chytac);
            x=x+20;
            if(x<650){
                Canvas.SetLeft(chytac,x);
            }   

        }
    }

    private void start_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        chytac.Visibility=Visibility.Visible;
        start.Visibility=Visibility.Collapsed;
    }

    private void random_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        Random random = new Random();
        //pocitadlo++;
        //score.Content=pocitadlo.ToString();
        telefon novy = new telefon();
        LayoutRoot.Children.Add(novy);
        Canvas.SetTop(novy, 0);
        Canvas.SetLeft(novy, random.Next(0,650));
        Canvas.SetZIndex(novy, -50);

        bomba bomba=new bomba();
        LayoutRoot.Children.Add(bomba);
        Canvas.SetTop(bomba, 0);
        Canvas.SetLeft(bomba, random.Next(0,650));
        Canvas.SetZIndex(bomba, -50);
    }
}


If you can represent your object as a rectangle (or set of rectangles) you can simply check if rectange(s) overlap with the white rectangle. Complex shapes might require more advanced algorithms. There are number of sprite libraries available such as Silversprite

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜