大家好我是C#编程的初学者,我需要有人来解释我如何改变精灵的颜色。一个人说我可以使用foreach
循环来做到这一点但是我得到了相同的颜色红色:
代码:
using System;
using System.Text;
using System.Drawing;
using System.Threading.Tasks;
using Ionic.Zlib;
using Sulakore.Protocol;
using Sulakore.Extensions;
using Habbogram.Camera;
using Habbogram.Communication;
namespace Habbogram
{
public partial class MainFrm : ExtensionForm
{
private const string PREVIEW_IMAGE_BASE_URL = "https://habbo-stories-content.s3.amazonaws.com/";
private readonly Random _colorGenerator;
private TaskCompletionSource<bool> _wasReplacedAwaitable;
private readonly Action<HPhoto> _onCameraPayloadIntercepted;
public CameraTriggers GramTriggers { get; }
public HPhoto CurrentPhoto { get; private set; }
public override bool IsExternal { get; } = true;
public MainFrm()
{
InitializeComponent();
_colorGenerator = new Random();
_wasReplacedAwaitable = new TaskCompletionSource<bool>();
_onCameraPayloadIntercepted = OnCameraPayloadIntercepted;
GramTriggers = new CameraTriggers(true);
GramTriggers.DetectIncoming = false;
Triggers = GramTriggers;
GramTriggers.CameraPayload += GramTriggers_CameraPayload;
}
private void UpdateInterface(HPhoto photo)
{
bool isReplacing = (photo != null);
CancelBtn.Enabled = (ReplaceBtn.Enabled = isReplacing);
PlanesTab.Text = $"Planes({photo?.Planes.Count ?? 0})";
SpritesTab.Text = $"Sprites({photo?.Sprites.Count ?? 0})";
FiltersTab.Text = $"Filters({photo?.Filters.Count ?? 0})";
SpriteTxt.DataSource = photo?.Sprites;
SpriteTxt.Enabled = (photo != null);
}
private Color GenerateRandomColor(byte alpha)
{
byte a = alpha;
int r = _colorGenerator.Next(0, 126);
int g = _colorGenerator.Next(0, 126);
int b = _colorGenerator.Next(0, 126);
return Color.FromArgb(alpha, r, g, b);
}
private void OnCameraPayloadIntercepted(HPhoto photo)
{
if (InvokeRequired) Invoke(_onCameraPayloadIntercepted, photo);
else UpdateInterface(photo);
}
private void CancelBtn_Click(object sender, EventArgs e)
{
_wasReplacedAwaitable.SetResult(false);
UpdateInterface(null);
}
private void ReplaceBtn_Click(object sender, EventArgs e)
{
_wasReplacedAwaitable.SetResult(true);
UpdateInterface(null);
}
private void InterceptChckbx_CheckedChanged(object sender, EventArgs e)
{
if (InterceptChckbx.Checked)
{
GramTriggers.CameraPayload += GramTriggers_CameraPayload;
}
else
{
GramTriggers.CameraPayload -= GramTriggers_CameraPayload;
UpdateInterface(null);
}
}
private void GramTriggers_CameraPayload(object sender, CameraPayloadEventArgs e)
{
try
{
e.ContinueRead();
byte[] jsonPayload = ZlibStream.UncompressBuffer(e.JsonPayload);
string json = Encoding.UTF8.GetString(jsonPayload);
CurrentPhoto = HPhoto.Create(json);
OnCameraPayloadIntercepted(CurrentPhoto);
foreach (Camera.Layers.Sprite s in CurrentPhoto.Sprites)
{
//Set color for all sprites in the photo.
s.Color = Color.FromArgb(1, 255, 1, 1).ToArgb();
}
// Wait for user to invoke a 'replace'.
bool wasReplaced = _wasReplacedAwaitable.Task.Result;
if (!wasReplaced) return; // User cancelled.
json = CurrentPhoto.ToJson();
byte[] compressedJson = Encoding.UTF8.GetBytes(json);
compressedJson = ZlibStream.CompressBuffer(compressedJson);
e.Replacement = new HMessage(e.Packet.Header,
compressedJson.Length, compressedJson);
}
finally
{
// Create new instance, so that we don't wait for an already completed task.
_wasReplacedAwaitable = new TaskCompletionSource<bool>();
}
}
}
}
答案 0 :(得分:0)
您忘记使用调用GenerateRandomColor()函数。您可以在foreach循环中调用该函数,也可以只复制以下代码并将其粘贴到foreach循环的位置。如果它满足,请给出这个答案。
foreach (Camera.Layers.Sprite s in CurrentPhoto.Sprites)
{
s.Color = Color.FromArgb(1, _colorGenerator.Next(1,256), _colorGenerator.Next(1,256), _colorGenerator.Next(1,256));
}
似乎s.Color采用Color数据类型...所以你不需要使用.ToArgb()。但是,如果确实需要int值,那么请在代码末尾添加.ToArgb()。