开发者

2D world in WPF

In XNA and other frameworks it's possible to define "2D开发者_如何学Python world", I'll use the term because it's easier to understand. For instance, Mario has two directions where he can go - right and left; if he moves right, the world right and left to him updates, so he can walk in a "2D Space" or world. I want to use that functionality in my application. How could I achieve that? Is there any good solution for this?


You may use ScrollViewer with hidden bars:

<Window x:Class="Scrolls.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" PreviewKeyDown="Window_PreviewKeyDown">
    <ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" x:Name="scroller">
        <TextBlock Text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."/>
    </ScrollViewer>
</Window>

code behind:

private void Window_PreviewKeyDown(object sender, KeyEventArgs e) {
    if (e.Key == Key.Right)
        scroller.ScrollToHorizontalOffset(scroller.HorizontalOffset + 1);
    else if (e.Key == Key.Left)
        scroller.ScrollToHorizontalOffset(scroller.HorizontalOffset - 1);
}

just put your 2D-world instead of <TextBlock .../>

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜