Ho appena letto e provato un ottimo esempio e spunto di partenza per creare sprite in silverlight. Ecco il codice che fa uso di caricamento dinamico di codice xaml:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
namespace SilverlightApplication11
{
public class Sprite : Control
{
private Image _spriteImage;
private RotateTransform _rotateTransform;
private int _width;
private int _height;
private double _posX = 0;
private double _posY = 0;
private double _xInc = 0;
private double _yInc = 0;
private int _opacityDir = 1;
private string _spriteTemplate =
"<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" +
" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" +
"<Image x:Name=\"SpriteImage\">" +
" <Image.RenderTransform>" +
" <RotateTransform x:Name=\"ImageTransform\">" +
" </RotateTransform>" +
" </Image.RenderTransform>" +
"</Image>" +
"</ControlTemplate>";
public Sprite(int width, int height)
{
_width = width;
_height = height;
Template = (ControlTemplate)XamlReader.Load(_spriteTemplate);
ApplyTemplate();
}
public override void OnApplyTemplate()
{
_spriteImage = (Image)GetTemplateChild("SpriteImage");
_rotateTransform = (RotateTransform)GetTemplateChild("ImageTransform");
_rotateTransform.CenterX = _width / 2;
_rotateTransform.CenterY = _height / 2;
}
public void SetImage(string resource)
{
Uri uri = new Uri(resource, UriKind.Relative);
ImageSource imgSrc = new System.Windows.Media.Imaging.BitmapImage(uri);
_spriteImage.Source = imgSrc;
}
public double YInc
{
set { _yInc = value; }
}
public double XInc
{
set { _xInc = value; }
}
public double PosX
{
get { return _posX; }
set
{
_posX = value;
this.SetValue(Canvas.LeftProperty, _posX);
}
}
public double PosY
{
get { return _posY; }
set
{
_posY = value;
this.SetValue(Canvas.TopProperty, _posY);
}
}
public bool Step(int speed)
{
if (_xInc * speed + _posX > 800 || _xInc * speed + _posX < 0)
return false;
if (_yInc * speed + PosY > 600 || _yInc * speed + PosY < 0)
return false;
PosX += _xInc * speed;
PosY += _yInc * speed;
if (_opacityDir == 1)
_spriteImage.Opacity += 0.01;
else
_spriteImage.Opacity -= 0.01;
if (_spriteImage.Opacity >= 1)
_opacityDir = 0;
else if (_spriteImage.Opacity <= 0)
_opacityDir = 1;
return true;
}
public void Rotate()
{
_rotateTransform.Angle += 1;
_rotateTransform.Transform(new Point(32, 24));
}
}
}
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Markup;
namespace SilverlightApplication11
{
public partial class Page : UserControl
{
Sprite _sprite;
public Page()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
this.KeyDown += new KeyEventHandler(Page_KeyDown);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
_sprite = new Sprite(50, 50);
_sprite.SetImage("GOKU.png");
_sprite.PosX = 350;
_sprite.PosY = 400;
MyCanvas.Children.Add(_sprite);
}
void Page_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Up:
_sprite.PosY -= 10;
break;
case Key.Down:
_sprite.PosY += 10;
break;
case Key.Left:
_sprite.PosX -= 10;
break;
case Key.Right:
_sprite.PosX += 10;
break;
default:
break;
}
}
}
}