最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - why does my data keeps the last element of previous array? - Stack Overflow

programmeradmin1浏览0评论

Hello Guys I'm new and trying to Make a RPG Text dialogue Style in Winforms.

you know the style which each char gets placed after another on.

but it keeps breaking

I have come so far:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Media;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio;
using NAudio.Wave;

namespace RandomRPG
{
    public class TextWrapper
    {
        string data;
        private bool isTyping;
        string directory = AppDomain.CurrentDomain.BaseDirectory;

        private int WordIndex;
        private string path;
        
        WMPLib.WindowsMediaPlayer Player;
        Random rnd = new Random();
        
        string[] dataWords;


        public async void ShowText(string[] text, Label textBox, int Delay)
        {

            if (!isTyping)
            {
                Player = new WMPLib.WindowsMediaPlayer();
                
                path = directory + @"\Data\Audio_Files\TextPop.wav";

                isTyping = true;
                textBox.Text = "";
                for (int i = 0; i < text.Length; i++)
                {

                    char[] characters = text[i].ToCharArray();
                    var words = text[i].Split(' ');
                    if (i > 0)
                    {
                        Array.Clear(dataWords,0,dataWords.Length);
                    }
                    
                    // Debug.Write("Words :"+words[WordIndex] + " " + WordIndex +" ");
                    
                        
                    for (int j = 0; j < characters.Length; j++)
                    {
                        data += characters[j];
                        dataWords = data.Split(' ');
                        textBox.Text = data;
                        
                        // if (dataWords.Last() == words[WordIndex])
                        // {
                        //     Player.URL = path;
                        //     // WordIndex++;
                        //     Player.controls.play();
                        //     WordIndex++;
                        // }
                        // Debug.WriteLine("ID" + WordIndex);
                        Debug.WriteLine(dataWords.Last());

                        await Task.Delay(Delay);
                    }
                    
                    
                    WordIndex = 0;

                }

                isTyping = false;
            }
            
        }
    }
}

so it worked and still does really good, BUT the problem is that when i tried to add sound for it it got worse.......

I want the sound to play after each word is typed...

string[] s = new[]
            {
                "Hello\n", "Welcome to this RPG Game\n",
                "In this Game you are in full control of what you can do\n", "The game is set in an open world map\n",
                "you can choose your Action in the Player_Action Panel\n",
                "If You are Ready Press {CONTINUE}"
            };
            _textWrapper.ShowText(s, Action_Window, 50); 

this is the void call in form1

when i look at console i get this:

He
Hel
Hell
Hello
Hello

Hello
W
Hello
We
Hello
Wel
Hello
Welc
Hello
Welco
Hello
Welcom
Hello
Welcome

t
to

t
th
thi
this

R
RP
RPG

G
Ga
Gam
Game
Game

Game
I
Game
In

t
th
thi
this

G
Ga
Gam
Game

y
yo
you

a
ar
are

i
in

f
fu
ful
full

c
co
con
cont
Started Thread 17448
contr
contro
control

o
of

w
wh
wha
what

y
yo
you

c
ca
can

d
do
do

do
T
do
Th
do
The

g
ga
gam
game

i
is

s
se
set

i
in

a
an

o
op
ope
open

w
wo
wor
worl
world

m
ma
map
map

map
y
map
yo
map
you

c
ca
can

c
ch
cho
choo
choos
choose

y
yo
you
your

A
Ac
Act
Acti
Actio
Action

i
in

t
th
the

P
Pl
Pla
Play
Playe
Player
Player_
Player_A
Player_Ac
Player_Act
Player_Acti
Player_Actio
Player_Action

P
Pa
Pan
Pane
Panel
Panel

Panel
I
Panel
If

Y
Yo
You

a
ar
are

R
Re
Rea
Read
Ready

P
Pr
Pre
Started Thread 9588
Pres
Press

{
{C
{CO
{CON
{CONT
{CONTI
{CONTIN
{CONTINU
{CONTINUE
{CONTINUE}

as you can see it's quite what i expect it to be but... if you look you can see at the start of a new line the last word of previous line will be also printed.

i assume it is because of how i manage data in TextWrapper

what do you think guys? how can i fix it?

Hello Guys I'm new and trying to Make a RPG Text dialogue Style in Winforms.

you know the style which each char gets placed after another on.

but it keeps breaking

I have come so far:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Media;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAudio;
using NAudio.Wave;

namespace RandomRPG
{
    public class TextWrapper
    {
        string data;
        private bool isTyping;
        string directory = AppDomain.CurrentDomain.BaseDirectory;

        private int WordIndex;
        private string path;
        
        WMPLib.WindowsMediaPlayer Player;
        Random rnd = new Random();
        
        string[] dataWords;


        public async void ShowText(string[] text, Label textBox, int Delay)
        {

            if (!isTyping)
            {
                Player = new WMPLib.WindowsMediaPlayer();
                
                path = directory + @"\Data\Audio_Files\TextPop.wav";

                isTyping = true;
                textBox.Text = "";
                for (int i = 0; i < text.Length; i++)
                {

                    char[] characters = text[i].ToCharArray();
                    var words = text[i].Split(' ');
                    if (i > 0)
                    {
                        Array.Clear(dataWords,0,dataWords.Length);
                    }
                    
                    // Debug.Write("Words :"+words[WordIndex] + " " + WordIndex +" ");
                    
                        
                    for (int j = 0; j < characters.Length; j++)
                    {
                        data += characters[j];
                        dataWords = data.Split(' ');
                        textBox.Text = data;
                        
                        // if (dataWords.Last() == words[WordIndex])
                        // {
                        //     Player.URL = path;
                        //     // WordIndex++;
                        //     Player.controls.play();
                        //     WordIndex++;
                        // }
                        // Debug.WriteLine("ID" + WordIndex);
                        Debug.WriteLine(dataWords.Last());

                        await Task.Delay(Delay);
                    }
                    
                    
                    WordIndex = 0;

                }

                isTyping = false;
            }
            
        }
    }
}

so it worked and still does really good, BUT the problem is that when i tried to add sound for it it got worse.......

I want the sound to play after each word is typed...

string[] s = new[]
            {
                "Hello\n", "Welcome to this RPG Game\n",
                "In this Game you are in full control of what you can do\n", "The game is set in an open world map\n",
                "you can choose your Action in the Player_Action Panel\n",
                "If You are Ready Press {CONTINUE}"
            };
            _textWrapper.ShowText(s, Action_Window, 50); 

this is the void call in form1

when i look at console i get this:

He
Hel
Hell
Hello
Hello

Hello
W
Hello
We
Hello
Wel
Hello
Welc
Hello
Welco
Hello
Welcom
Hello
Welcome

t
to

t
th
thi
this

R
RP
RPG

G
Ga
Gam
Game
Game

Game
I
Game
In

t
th
thi
this

G
Ga
Gam
Game

y
yo
you

a
ar
are

i
in

f
fu
ful
full

c
co
con
cont
Started Thread 17448
contr
contro
control

o
of

w
wh
wha
what

y
yo
you

c
ca
can

d
do
do

do
T
do
Th
do
The

g
ga
gam
game

i
is

s
se
set

i
in

a
an

o
op
ope
open

w
wo
wor
worl
world

m
ma
map
map

map
y
map
yo
map
you

c
ca
can

c
ch
cho
choo
choos
choose

y
yo
you
your

A
Ac
Act
Acti
Actio
Action

i
in

t
th
the

P
Pl
Pla
Play
Playe
Player
Player_
Player_A
Player_Ac
Player_Act
Player_Acti
Player_Actio
Player_Action

P
Pa
Pan
Pane
Panel
Panel

Panel
I
Panel
If

Y
Yo
You

a
ar
are

R
Re
Rea
Read
Ready

P
Pr
Pre
Started Thread 9588
Pres
Press

{
{C
{CO
{CON
{CONT
{CONTI
{CONTIN
{CONTINU
{CONTINUE
{CONTINUE}

as you can see it's quite what i expect it to be but... if you look you can see at the start of a new line the last word of previous line will be also printed.

i assume it is because of how i manage data in TextWrapper

what do you think guys? how can i fix it?

Share Improve this question asked Mar 19 at 10:28 Mr PerfectMr Perfect 1 5
  • Because you only splitted the text with a space. – shingo Commented Mar 19 at 10:45
  • what should i split it with then? \n? – Mr Perfect Commented Mar 19 at 10:53
  • 1 Too many things are going on. Here is a much simpler version without re-entrancy check. – Sinatr Commented Mar 19 at 10:56
  • The title says "why does my data keeps the last element of previous array?" so I was expecting the main focus of your question to be that. But after reading your question, the prominent remark was "the problem is that when i tried to add sound for it it got worse", but then you make a third claim that "at the start of a new line the last word of previous line will be also printed". So which of these is the real question? – Wyck Commented Mar 19 at 13:19
  • See: Specify multiple separators and include space and \n. Or you may use Trim before splitting to ignore leading and trailing white space characters. – Olivier Jacot-Descombes Commented Mar 19 at 13:40
Add a comment  | 

2 Answers 2

Reset to default 0

Hello again I managed to solve the problem.

as shingo suggested i splitted the words by '\n' not by 'space'

                        dataWords = data.Split('\n');

and the used another array of string to splitted yet another time but this time by 'space':

for (int j = 0; j < dataWords.Length; j++)
                        {
                            wordList = dataWords[j].Split(' ');
                        }

and that's it it worked..

try this

for (int i = 0; i < text.Length; i++)

 {

     data = "";

      char[] characters = text[i].ToCharArray();

     var words = text[i].Split(' ');

      for (int j = 0; j < characters.Length; j++)     { 

        data += characters[j]; 

        dataWords = data.Split(' ');

         textBox.Text += data;

          await Task.Delay(Delay);

     } } 

 
发布评论

评论列表(0)

  1. 暂无评论