1

这是关于我在同一个项目中遇到的问题的后续问题:在我的图形方法中(我使用它作为更新方法)我正在绘制一个地图图像,我已经在本地存储在调试文件夹中. 然而,当点击地图上的某个区域时,地图应该消失并被 3 张图像取代。但是我遇到的问题是,实际上一次只绘制一张图像(最后一张)。无论如何,这是我的图形方法:

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        //load the map image
        Image map = Image.FromFile("map.png");
        SolidBrush b = new SolidBrush(Color.Red);

         //now I need to add the paint controls for is hit and not hit
        if (isHit == true && goBack == false)
        {
            g.DrawImage(backButton, new Point(0, 0));
            for (int i = 0; i < trees.Length; i++)
            {
                if (trees[i].isHit == true)
                {   
                    Tree t = trees[i];
                    Image treeImage1, treeImage2;
                    treeImage1 = Image.FromFile(t.EnglishName + ".png");
                    treeImage2 = Image.FromFile(t.EnglishName + "leaf.png");
                    g.DrawImage(treeImage1, new Point(10,20);
                    g.DrawImage(treeImage2, new Point((treeImage1.Width + 10), 20);
                    loadTree(trees[i]);                        
                }
            }
            //update labels for text and info                 
            // draw a back button(add control to click, use image)
        }
        else if(isHit == false)
        {
            g.DrawImage(backButton, new Point(0, 0));
            g.DrawImage(map, new Point(0, 0));

            for (int i = 0; i < trees.Length; i++)
            {
                g.FillRectangle(b, trees[i].X, trees[i].Y, 3, 3);

            }

        }
        this.Refresh();

    }

到目前为止我发现的一个问题是,只要语句为真(由于某种未知原因),就永远不会到达第一个 if 语句。这些变量在我的鼠标单击事件中控制如下:

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        mouseX = e.X;
        mouseY = e.Y;
        bool isClicked = false;
        for (int i = 0; i < trees.Length; i++)
        {
            isClicked = trees[i].checkClick(mouseX, mouseY);
            if (isClicked)
            {
                isHit = isClicked;
                if(mouseX >= 5 && mouseX <= backButton.Width)
                {                        
                    if(mouseY >= 5 & mouseY <= backButton.Height)
                    {
                        goBack = true;
                        trees[i].isHit = false;
                        isHit = false;
                        HideLabels();
                    }
                }
                for (int j = 0; j < trees.Length; j++) 
                {
                    if (trees[j] != trees[i]) 
                    {
                        trees[j].isHit = false;
                    }
                }
                trees[i].isHit = true;
                loadTree(trees[i]);

            }
        }
    }

任何和所有的帮助将不胜感激。如果您需要更多信息,请告诉我,我会尽快发布谢谢 :)

4

2 回答 2

0

“但是我遇到的问题是,实际上一次只绘制了一张图像(最后一张)。”

在你的 hit 例程中,每当一棵树被击中时,你都会使用这个循环遍历并重置所有其他树:

            for (int j = 0; j < trees.Length; j++) 
            {
                if (trees[j] != trees[i]) 
                {
                    trees[j].isHit = false;
                }
            }

因此,只有最后一棵树是真实的。

我们不知道您的系统是什么样子(或它应该如何运行),但一个可能的解决方案是在检查所有树之前使用该循环一次,这样它们都开始为假,只有命中的树被切换。也许是这样的:

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
    // reset all the trees:
    for (int j = 0; j < trees.Length; j++) 
    {
        trees[j].isHit = false;
    }

    // see which trees are hit:
    mouseX = e.X;
    mouseY = e.Y;
    bool isClicked = false;
    for (int i = 0; i < trees.Length; i++)
    {
        isClicked = trees[i].checkClick(mouseX, mouseY);
        if (isClicked)
        {
            // initially true, but might be reset below if the 
            // back button was hit instead?
            trees[i].isHit = true;

            isHit = isClicked;
            if(mouseX >= 5 && mouseX <= backButton.Width)
            {                        
                if(mouseY >= 5 & mouseY <= backButton.Height)
                {
                    goBack = true;
                    trees[i].isHit = false;
                    isHit = false;
                    HideLabels();
                }
            }

            loadTree(trees[i]);
        }
    }
}
于 2013-06-09T15:53:13.250 回答
0

问题实际上出在绘制后退按钮的那一行。画布没有按比例缩小,并且设置为大于应用程序的大小,因此除非您最大化应用程序,否则它将始终如此。成功裁剪后,一切正常,使用我原来的方法

于 2013-06-09T22:02:42.827 回答